Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverchang committed Jan 7, 2025
1 parent a48ad3b commit cbd9767
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 4 additions & 4 deletions experimental/javareach/cmd/reachable/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// and automatically downloading building and downloading dependencies if the
// pom.xml exists in the root .jar file.
func main() {
classPath := flag.String("classpath", "", "(Required) A single directory containing class files to look for.")
classPath := flag.String("classpath", "", "(Required) A single root directory containing Java class files.")

flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s <arguments> <root class name>\n", os.Args[0])
Expand All @@ -40,7 +40,7 @@ func main() {
for _, className := range flag.Args() {
cf, err := findClass(*classPath, className)
if err != nil {
log.Fatalf("Failed to find class %s", className)
log.Fatalf("Failed to find class %s: %v", className, err)
}

EnumerateReachability(cf, *classPath)
Expand Down Expand Up @@ -79,7 +79,7 @@ func enumerateReachability(cf *javareach.ClassFile, classPath string, seen map[s
if _, ok := seen[thisClass]; ok {
return nil
}
fmt.Printf("this class: %s\n", thisClass)
fmt.Printf("analyzing class: %s\n", thisClass)
seen[thisClass] = struct{}{}

for i, cp := range cf.ConstantPool {
Expand All @@ -93,12 +93,12 @@ func enumerateReachability(cf *javareach.ClassFile, classPath string, seen map[s
if err != nil {
return err
}
fmt.Printf("class: %s\n", class)

if strings.HasPrefix(class, "java/") || strings.HasPrefix(class, "javax/") {
// Standard library
continue
}
fmt.Printf("class dependency: %s\n", class)

depcf, err := findClass(classPath, class)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions experimental/javareach/javaclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,16 @@ func ParseClass(r io.Reader) (*ClassFile, error) {
switch kind {
case ConstantKindUtf8:
constant := &ConstantUtf8{}
// todo: limit len
err := binary.Read(r, binary.BigEndian, &constant.Length)
if err != nil {
return nil, err
}

const maxConstantLength = 32 * 1024
if constant.Length > maxConstantLength {
return nil, fmt.Errorf("constant size too large (%d)", constant.Length)
}

constant.Bytes = make([]byte, constant.Length)
_, err = r.Read(constant.Bytes)
if err != nil {
Expand Down Expand Up @@ -332,14 +336,15 @@ func ParseClass(r io.Reader) (*ClassFile, error) {
}
cp = constant
default:
return nil, errors.New("invalid cp_info type")
return nil, fmt.Errorf("invalid cp_info type %d at index %d", kind, i+1)
}

cf.ConstantPool = append(cf.ConstantPool, cp)

if cp.Type() == ConstantKindDouble || cp.Type() == ConstantKindLong {
// 8-byte values take up 2 constant pool entries.
cf.ConstantPool = append(cf.ConstantPool, &ConstantPlaceholder{})
i += 1
}
}

Expand Down

0 comments on commit cbd9767

Please sign in to comment.