Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PrimAndStringOp bug in AstJavaTypeInference #12

Merged

Conversation

vazexqi
Copy link

@vazexqi vazexqi commented Mar 29, 2013

Problem

I was using AstJavaTypeInference and discovered a bug with the way that string + primitives are handled. I've included a test case in the pull request but here's the gist of it

package javaonepointfive;

public class TypeInferencePrimAndStringOp {
  public static void main(String[] args) {
    int a = 2;
    String result = "a" + a;
  }
}

Trying to infer the type of result ends with a Unexpected type: class com.ibm.wala.analysis.typeInference.JavaPrimitiveType exception.

Cause

After tracing it down, I found that PrimAndStringOp uses a field call stringClass in AstJavaTypeInference but that field does not get a chance to be initialized before it is used. That is because the first line in AstJavaTypeInference is a call to super that initializes the AbstractFixedPointSolver, which calls the initialize() and solve() functions, which in turn, calls the evaluate function in PrimAndStringOp.

Here's the original constructor for AstJavaTypeInference:

  public AstJavaTypeInference(IR ir, IClassHierarchy cha, boolean doPrimitives) {
    super(ir, cha, JavaPrimitiveType.BOOLEAN, doPrimitives);
    this.stringClass = cha.lookupClass(TypeReference.JavaLangString);

(Possible) Solution

I used lazy initialization for stringClass to ensure that it is initialized when it is first used in the evaluate function in PrimAndStringOp.

  IClass getStringClass() {
    if (stringClass == null) {
      this.stringClass = cha.lookupClass(TypeReference.JavaLangString);
    }
    return stringClass;
  }

Nicholas Chen added 2 commits March 29, 2013 17:21
…ypeInference.JavaPrimitiveType

This is a simple test case that illustrates the issue with type inference
on a String + some primitive.

The code is

package javaonepointfive;

public class TypeInferencePrimAndStringOp {
  public static void main(String[] args) {
    int a = 2;
    String result = "a" + a;
  }
}
This ensures that when we need to use stringClass, it can be constructed
directly from the cha field. Previously, it was only initialized after
the call to super(…) in the constructor. However, by then it is too late
because super(…) calls initialize() and solve() which need the
stringClass field.
msridhar added a commit that referenced this pull request Mar 29, 2013
…gOp-bug

PrimAndStringOp bug in AstJavaTypeInference
@msridhar msridhar merged commit e70907a into wala:master Mar 29, 2013
@msridhar
Copy link
Member

Nice report and fix; merged. Thanks!

msridhar added a commit that referenced this pull request Mar 30, 2013
…gOp-bug

Add the test file to com.ibm.wala.cast.java.test.data/src for pull request #12
msridhar added a commit that referenced this pull request Aug 7, 2013
…gOp-bug

PrimAndStringOp bug in AstJavaTypeInference
msridhar added a commit that referenced this pull request Aug 7, 2013
…gOp-bug

Add the test file to com.ibm.wala.cast.java.test.data/src for pull request #12
deepkiransangha pushed a commit to deepkiransangha/WALA that referenced this pull request Jan 27, 2025
tejkart pushed a commit to tejkart/WALA that referenced this pull request Jan 27, 2025
tejkart pushed a commit to tejkart/WALA that referenced this pull request Jan 27, 2025
mh4535 pushed a commit to mh4535/WALA that referenced this pull request Jan 27, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants