Skip to content

Commit 495e369

Browse files
damyanpCopilot
andauthored
Fix crash for diagnostics around source with embedded nulls (microsoft#8164)
Previously if the source contained embedded nulls and a diagnostic range included that this would result in an assert firing (and then a crash in builds without asserts). This change makes it quietly return in this case, and adds a regression test for this situation. --------- Co-authored-by: Copilot <[email protected]>
1 parent ba40d77 commit 495e369

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

tools/clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,10 @@ static void highlightRange(const CharSourceRange &R,
965965
EndColNo = map.startOfPreviousColumn(EndColNo);
966966

967967
// If the start/end passed each other, then we are trying to highlight a
968-
// range that just exists in whitespace, which must be some sort of other
969-
// bug.
970-
assert(StartColNo <= EndColNo && "Trying to highlight whitespace??");
968+
// range that just exists in whitespace. This can happen with null bytes or
969+
// other unusual characters in the source.
970+
if (StartColNo > EndColNo)
971+
return;
971972
}
972973

973974
assert(StartColNo <= map.getSourceLine().size() && "Invalid range!");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Regression test: dxc crashes on HLSL with embedded null bytes due to
2+
// out-of-bounds access in highlightRange() (TextDiagnostic.cpp).
3+
4+
// RUN: python -c "import sys; sys.stdout.buffer.write(b'f(){a b int4\x00(1}')" > %t.hlsl
5+
// RUN: not %dxc /T ps_6_0 %t.hlsl 2>&1 | FileCheck %s
6+
7+
// CHECK: error: HLSL requires a type specifier for all declarations

0 commit comments

Comments
 (0)