M2-Planet icon indicating copy to clipboard operation
M2-Planet copied to clipboard

`'`/`"` on `//` line can start string parsing

Open gtker opened this issue 11 months ago • 1 comments

// '

fails with Unterminated string.

// "

Fails with the same message.

Adding a second // ' or ' after the first does not fail.

This isn't a big deal, but it will cause weird failures if single line comments are used and somebody writes don't or something else that uses the apostrophe.

gtker avatar Apr 08 '25 13:04 gtker

This is because of how // is tokenized. Because of the // CONSTANT 1 syntax anything after // is tokenized like normal.

This can be used for a rather fun exploit where you can arbitrarily remove code in a very unintuitive way.

For example, the following program will always return 1 and the output will not have an if statement in it.

int main() {
    int a = 0;
    // Will always happen, so wont't need rest of function
    if(a == 0) return 0;

    // Won't happen
    return 1;
}

This behavior is also present in at least cc_amd64 and most likely all other cc_* compilers.

gtker avatar Apr 08 '25 14:04 gtker