Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

new expression parsing failure #8467

Open
MFaisalZaki opened this issue Mar 16, 2022 · 6 comments
Open

new expression parsing failure #8467

MFaisalZaki opened this issue Mar 16, 2022 · 6 comments
Labels
question Further information is requested

Comments

@MFaisalZaki
Copy link

CodeQL fails to parse new expressions like:

  short s;
  long *lp = ::new (&s) long;

This issue arose when trying to implement SEI CERT C++ rules using CodeQL.

@MFaisalZaki MFaisalZaki added the question Further information is requested label Mar 16, 2022
@geoffw0
Copy link
Contributor

geoffw0 commented Mar 17, 2022

What exactly is the behaviour you want that you are not seeing? The call to ::new should be accessible as a NewExpr in CodeQL, and you can use getPlacementPointer() to get the &s.

@MFaisalZaki
Copy link
Author

When I am trying to run this query

from NewOrNewArrayExpr fncall
select fncall.getAllocatorCall().getAnArgument()

in order to check the allocation type long matches the enclosing variable type it generates an <error expr> and the &s.

@MathiasVP
Copy link
Contributor

Thanks for reporting this issue @MFaisalZaki. I've created an internal issue for tracking this.

@jketema
Copy link
Contributor

jketema commented Mar 17, 2022

Hi @MFaisalZaki,

Just for my understanding. When you tried to parse the placement new from your example, did you also #include <new>? This is generally also needed with compilers like gcc and clang. They will also not parse the code without it.

When I locally try this and #include <new>, the fragment parses for me.

@MFaisalZaki
Copy link
Author

@jketema That's true; you must include #include <new> to compile the code. However, I have used the MSVC compiler, not GCC/clang.

@jketema
Copy link
Contributor

jketema commented Mar 18, 2022

Hi @MFaisalZaki

Thanks for the clarification. There's definitely an issue here, which we'll keep tracking.

in order to check the allocation type long matches the enclosing variable type it generates an <error expr> and the &s.

Note that the <error expr> takes the place of the size argument of the new operator, so even if it was correctly captured, it would not give you the long type, but only its size.

To compare the allocation type and the type of the placement pointer, a better approach is to use the predicates directly defined on NewExpr, as @geoffw0 already suggests, and then look at the underlying types:

import cpp

from NewExpr new, Expr placementPtr
where placementPtr = new.getPlacementPointer()
select placementPtr, placementPtr.getUnderlyingType().(PointerType).getBaseType(),
  new.getAllocatedType().getUnderlyingType()

For your example this give me:

| test.cpp:64:21:64:22 | & ... | file://:0:0:0:0 | short | file://:0:0:0:0 | long |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants