Skip to content

Commit c69a781

Browse files
authored
Prevent undefined behavior with --cpp-lines (#29041)
Fixes an issue where `--cpp-lines` could attempt to index an empty string with `-1`. In some cases, `cp` could be `""` and `strlen(cp)` would be 0. This means we would attempt to index the array with a negative value (undefined behavior!) [Reviewed by @arifthpe]
2 parents f9916f4 + 98313bb commit c69a781

1 file changed

Lines changed: 2 additions & 5 deletions

File tree

compiler/backend/beautify.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,8 @@ void beautify(fileinfo* origfile) {
227227
}
228228
fprintf(outputfile, ZLINEFORMAT, zlineP, zname);
229229
}
230-
}
231-
232-
if (cp[strlen(cp)-1] == '\n')
233-
new_line = TRUE;
234-
else {
230+
new_line = cp[strlen(cp)-1] == '\n';
231+
} else {
235232
new_line = FALSE;
236233
}
237234

0 commit comments

Comments
 (0)