Skip to content

C: preprocessor branches truncated to first line #18842

Open
@randomdude

Description

Hi all. I'm seeing some unexpected behavior from CodeQL when I process C preprocessor branches that span multiple lines.

For example, if I scan the following C file:

#if defined(FOO) || \
     defined(BAR)
#endif

with the following query:

from PreprocessorBranchDirective pbd select pbd, pbd.toString()

I see the output:

"foo","foo","error","#if defined(FOO) || \","/a.c","1","1","1","21"
"foo","foo","error","#endif","/a.c","3","1","3","6"

I expected to see the entire multiline preprocessor condition in the output of the .toString(), rather than just the first line - but when I went to file this bug, I realised this is not specified anywhere as correct behavior (as far as I can see), and so this may be expected behavior. However, if we ignore that maybe-issue, there is still a second issue - the location information appears to be incorrect, stating that the preprocessor condition ends at line 1 column 21, while it actually ends on line 2.

Finally - it may be that the CSV output is malformed, as the terminating quote is prefixed by a backslash.

Hope this is helpful! For your extra context, I found this while scanning the Linux kernel, so it seems this is a common code pattern, as opposed to an infrequently-seen corner case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions