Fix a SARIF exporter crash with macro expansions
authorJoe Ranieri <jranieri@grammatech.com>
Tue, 27 Aug 2019 14:20:27 +0000 (14:20 +0000)
committerJoe Ranieri <jranieri@grammatech.com>
Tue, 27 Aug 2019 14:20:27 +0000 (14:20 +0000)
Differential Revision: https://reviews.llvm.org/D65209

llvm-svn: 370061

clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp
clang/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif
clang/test/Analysis/diagnostics/sarif-multi-diagnostic-test.c

index 3b4667765c2e2c9f671857cdc54b47c525984a0e..dd66d902d9da01cea550568d5fa1f7a42f323990 100644 (file)
@@ -219,9 +219,10 @@ static json::Object createThreadFlow(const PathPieces &Pieces,
   for (const auto &Piece : Pieces) {
     const PathDiagnosticLocation &P = Piece->getLocation();
     Locations.push_back(createThreadFlowLocation(
-        createLocation(createPhysicalLocation(P.asRange(),
-                                              *P.asLocation().getFileEntry(),
-                                              SMgr, Files),
+        createLocation(createPhysicalLocation(
+                           P.asRange(),
+                           *P.asLocation().getExpansionLoc().getFileEntry(),
+                           SMgr, Files),
                        Piece->getString()),
         calculateImportance(*Piece)));
   }
@@ -255,7 +256,8 @@ static json::Object createResult(const PathDiagnostic &Diag, json::Array &Files,
       {"locations",
        json::Array{createLocation(createPhysicalLocation(
            Diag.getLocation().asRange(),
-           *Diag.getLocation().asLocation().getFileEntry(), SMgr, Files))}},
+           *Diag.getLocation().asLocation().getExpansionLoc().getFileEntry(),
+           SMgr, Files))}},
       {"ruleIndex", Iter->getValue()},
       {"ruleId", Diag.getCheckName()}};
 }
index 41a7c062447c4e2d37fc768a9affb4f6ead960c9..7aa983102c7eb341a4e56ebccfb5e44474d44e6c 100644 (file)
@@ -6,7 +6,7 @@
         {
           "fileLocation": {
           },
-          "length": 686,
+          "length": 951,
           "mimeType": "text/plain",
           "roles": [
             "resultFile"
             "name": {
               "text": "core.DivideZero"
             }
+          },
+          {
+            "fullDescription": {
+              "text": "Check for memory leaks, double free, and use-after-free problems. Traces memory managed by malloc()/free()."
+            },
+            "helpUri": "https://clang-analyzer.llvm.org/available_checks.html#unix.Malloc",
+            "id": "unix.Malloc",
+            "name": {
+              "text": "unix.Malloc"
+            }
           }
         ]
       },
@@ -65,9 +75,9 @@
                           },
                           "region": {
                             "endColumn": 6,
-                            "endLine": 24,
+                            "endLine": 34,
                             "startColumn": 3,
-                            "startLine": 24
+                            "startLine": 34
                           }
                         }
                       }
@@ -84,9 +94,9 @@
                           },
                           "region": {
                             "endColumn": 18,
-                            "endLine": 9,
+                            "endLine": 11,
                             "startColumn": 11,
-                            "startLine": 9
+                            "startLine": 11
                           }
                         }
                       }
                 },
                 "region": {
                   "endColumn": 18,
-                  "endLine": 9,
+                  "endLine": 11,
                   "startColumn": 11,
-                  "startLine": 9
+                  "startLine": 11
                 }
               }
             }
                           },
                           "region": {
                             "endColumn": 6,
-                            "endLine": 25,
+                            "endLine": 35,
                             "startColumn": 3,
-                            "startLine": 25
+                            "startLine": 35
                           }
                         }
                       }
                           },
                           "region": {
                             "endColumn": 11,
-                            "endLine": 13,
+                            "endLine": 15,
                             "startColumn": 3,
-                            "startLine": 13
+                            "startLine": 15
                           }
                         }
                       }
                           },
                           "region": {
                             "endColumn": 9,
-                            "endLine": 14,
+                            "endLine": 16,
                             "startColumn": 3,
-                            "startLine": 14
+                            "startLine": 16
                           }
                         }
                       }
                 },
                 "region": {
                   "endColumn": 9,
-                  "endLine": 14,
+                  "endLine": 16,
                   "startColumn": 3,
-                  "startLine": 14
+                  "startLine": 16
                 }
               }
             }
                           },
                           "region": {
                             "endColumn": 13,
-                            "endLine": 18,
+                            "endLine": 20,
                             "startColumn": 7,
-                            "startLine": 18
+                            "startLine": 20
                           }
                         }
                       }
                           "region": {
                             "endColumn": 3,
                             "startColumn": 3,
-                            "startLine": 18
+                            "startLine": 20
                           }
                         }
                       }
                           "region": {
                             "endColumn": 14,
                             "startColumn": 14,
-                            "startLine": 19
+                            "startLine": 21
                           }
                         }
                       }
                 "region": {
                   "endColumn": 14,
                   "startColumn": 14,
-                  "startLine": 19
+                  "startLine": 21
                 }
               }
             }
           },
           "ruleId": "core.DivideZero",
           "ruleIndex": 2
+        },
+        {
+          "codeFlows": [
+            {
+              "threadFlows": [
+                {
+                  "locations": [
+                    {
+                      "importance": "essential",
+                      "location": {
+                        "message": {
+                          "text": "Memory is allocated"
+                        },
+                        "physicalLocation": {
+                          "fileLocation": {
+                            "fileIndex": 0,
+                          },
+                          "region": {
+                            "endColumn": 24,
+                            "endLine": 26,
+                            "startColumn": 15,
+                            "startLine": 26
+                          }
+                        }
+                      }
+                    },
+                    {
+                      "importance": "important",
+                      "location": {
+                        "message": {
+                          "text": "Assuming 'i' is < 4"
+                        },
+                        "physicalLocation": {
+                          "fileLocation": {
+                            "fileIndex": 0,
+                          },
+                          "region": {
+                            "endColumn": 12,
+                            "endLine": 27,
+                            "startColumn": 7,
+                            "startLine": 27
+                          }
+                        }
+                      }
+                    },
+                    {
+                      "importance": "unimportant",
+                      "location": {
+                        "message": {
+                          "text": "Taking true branch"
+                        },
+                        "physicalLocation": {
+                          "fileLocation": {
+                            "fileIndex": 0,
+                          },
+                          "region": {
+                            "endColumn": 3,
+                            "startColumn": 3,
+                            "startLine": 27
+                          }
+                        }
+                      }
+                    },
+                    {
+                      "importance": "essential",
+                      "location": {
+                        "message": {
+                          "text": "Potential leak of memory pointed to by 'mem'"
+                        },
+                        "physicalLocation": {
+                          "fileLocation": {
+                            "fileIndex": 0,
+                          },
+                          "region": {
+                            "endColumn": 12,
+                            "startColumn": 12,
+                            "startLine": 28
+                          }
+                        }
+                      }
+                    }
+                  ]
+                }
+              ]
+            }
+          ],
+          "locations": [
+            {
+              "physicalLocation": {
+                "fileLocation": {
+                  "fileIndex": 0,
+                },
+                "region": {
+                  "endColumn": 12,
+                  "startColumn": 12,
+                  "startLine": 28
+                }
+              }
+            }
+          ],
+          "message": {
+            "text": "Potential leak of memory pointed to by 'mem'"
+          },
+          "ruleId": "unix.Malloc",
+          "ruleIndex": 3
         }
       ],
       "tool": {
index 3402714a30ae736193d760c4550795273efc8ba2..99c8d2faf1cb05146e65e7790cbcfd287169ad13 100644 (file)
@@ -1,5 +1,7 @@
-// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.security.taint,debug.TaintTest %s -verify -analyzer-output=sarif -o - | %normalize_sarif | diff -U1 -b %S/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif -
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.security.taint,debug.TaintTest,unix.Malloc %s -verify -analyzer-output=sarif -o - | %normalize_sarif | diff -U1 -b %S/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif -
 #include "../Inputs/system-header-simulator.h"
+#include "../Inputs/system-header-simulator-for-malloc.h"
+#define ERR -1
 
 int atoi(const char *nptr);
 
@@ -20,10 +22,19 @@ int h(int i) {
   return 0;
 }
 
+int leak(int i) {
+  void *mem = malloc(8);
+  if (i < 4)
+    return ERR; // expected-warning {{Potential leak of memory pointed to by 'mem'}}
+  free(mem);
+  return 0;
+}
+
 int main(void) {
   f();
   g();
   h(0);
+  leak(0);
   return 0;
 }