Skip to content

[C] How to query the variable expression used in array initialization? #14346

Answered by mbg
hyunsooda asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @hyunsooda 👋🏻

Thank you for your question! You can get hold of the expression used to dynamically set the size of an array through the VlaDimensionStmt type. For example:

from VlaDimensionStmt s
select s, s.getDimensionExpr()

If run on your sample code, this will return one result where getDimensionExpr() points at the expression of just myval.

We expose a getValue() predicate on expressions, but that will only return a result if the value is constant, which it is not here. We have experimental experimental.semmle.code.cpp.rangeanalysis.ArrayLengthAnalysis and experimental.semmle.code.cpp.rangeanalysis.InBoundsPointerDeref libraries which might be of interest to you.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hyunsooda
Comment options

Answer selected by hyunsooda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants