[PATCH] D63786: Print NULL as "(null)" in diagnostic message

Andus Yu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 25 13:36:02 PDT 2019


andusy created this revision.
andusy added reviewers: hubert.reinterpretcast, xingxue, jasonliu, daltenty, cebowleratibm.
andusy added a project: clang.
Herald added subscribers: jsji, arphaman.

Passing a null pointer to the `printf` family for a %s format specifier leads to undefined behaviour. The tests currently expect `(null)`. Explicitly test for a null pointer and provide the expected string.


Repository:
  rC Clang

https://reviews.llvm.org/D63786

Files:
  clang/tools/c-index-test/c-index-test.c


Index: clang/tools/c-index-test/c-index-test.c
===================================================================
--- clang/tools/c-index-test/c-index-test.c
+++ clang/tools/c-index-test/c-index-test.c
@@ -1053,7 +1053,8 @@
     if (Cursor.kind == CXCursor_InclusionDirective) {
       CXFile File = clang_getIncludedFile(Cursor);
       CXString Included = clang_getFileName(File);
-      printf(" (%s)", clang_getCString(Included));
+      const char *IncludedString = clang_getCString(Included);
+      printf(" (%s)", IncludedString ? IncludedString : "(null)");
       clang_disposeString(Included);
       
       if (clang_isFileMultipleIncludeGuarded(TU, File))
@@ -4644,18 +4645,18 @@
     CXFile File;
     CXString FileName, DiagSpelling, DiagOption, DiagCat;
     unsigned line, column, offset;
-    const char *DiagOptionStr = 0, *DiagCatStr = 0;
-    
+    const char *DiagOptionStr = 0, *DiagCatStr = 0, *FileNameStr = 0;
     D = clang_getDiagnosticInSet(Diags, i);
     DiagLoc = clang_getDiagnosticLocation(D);
     clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
     FileName = clang_getFileName(File);
+    FileNameStr = clang_getCString(FileName);
     DiagSpelling = clang_getDiagnosticSpelling(D);
     
     printIndent(indent);
-    
+
     fprintf(stderr, "%s:%d:%d: %s: %s",
-            clang_getCString(FileName),
+            FileNameStr ? FileNameStr : "(null)",
             line,
             column,
             getSeverityString(clang_getDiagnosticSeverity(D)),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63786.206514.patch
Type: text/x-patch
Size: 1521 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190625/fc7ced12/attachment.bin>


More information about the cfe-commits mailing list