]> granicus.if.org Git - clang/commitdiff
InstrProf: Read unsigned numbers with strtoul and strtoull
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Thu, 10 Apr 2014 23:37:34 +0000 (23:37 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Thu, 10 Apr 2014 23:37:34 +0000 (23:37 +0000)
Fixes a bug where unsigned numbers are read using strtol and strtoll.

I don't have a testcase because this bug is effectively unobservable
right now.  To expose the problem in the hash, we would need a function
with greater than INT64_MAX counters, which we don't handle anyway.  To
expose the problem in the function count, we'd need a function with
greater than INT32_MAX counters; this is theoretically observable, but
it isn't a practical testcase to check in.

An upcoming commit changes the hash to be non-trivial, so we'll get some
coverage eventually.

<rdar://problem/16435801>

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@206001 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CodeGenPGO.cpp

index 2e562ca826f36a2b7c252aa6b0f739fb7b9cc8e2..36edf638cf43354b74a728f0ea97d8e5c28c9614 100644 (file)
@@ -15,7 +15,7 @@
 #include "CodeGenFunction.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/AST/StmtVisitor.h"
-#include "llvm/Config/config.h" // for strtoull()/strtoll() define
+#include "llvm/Config/config.h" // for strtoull()/strtoul() define
 #include "llvm/IR/MDBuilder.h"
 #include "llvm/Support/FileSystem.h"
 
@@ -69,7 +69,7 @@ PGOProfileData::PGOProfileData(CodeGenModule &CGM, std::string Path)
 
     // Read the number of counters.
     char *EndPtr;
-    unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10);
+    unsigned NumCounters = strtoul(++CurPtr, &EndPtr, 10);
     if (EndPtr == CurPtr || *EndPtr != '\n' || NumCounters <= 0) {
       ReportBadPGOData(CGM, "pgo data file has unexpected number of counters");
       return;
@@ -77,7 +77,7 @@ PGOProfileData::PGOProfileData(CodeGenModule &CGM, std::string Path)
     CurPtr = EndPtr;
 
     // Read function count.
-    uint64_t Count = strtoll(CurPtr, &EndPtr, 10);
+    uint64_t Count = strtoull(CurPtr, &EndPtr, 10);
     if (EndPtr == CurPtr || *EndPtr != '\n') {
       ReportBadPGOData(CGM, "pgo-data file has bad count value");
       return;
@@ -119,13 +119,13 @@ bool PGOProfileData::getFunctionCounts(StringRef FuncName, uint64_t &FuncHash,
 
   char *EndPtr;
   // Read the function hash.
-  FuncHash = strtoll(++CurPtr, &EndPtr, 10);
+  FuncHash = strtoull(++CurPtr, &EndPtr, 10);
   assert(EndPtr != CurPtr && *EndPtr == '\n' &&
          "pgo-data file has corrupted function hash");
   CurPtr = EndPtr;
 
   // Read the number of counters.
-  unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10);
+  unsigned NumCounters = strtoul(++CurPtr, &EndPtr, 10);
   assert(EndPtr != CurPtr && *EndPtr == '\n' && NumCounters > 0 &&
          "pgo-data file has corrupted number of counters");
   CurPtr = EndPtr;
@@ -134,7 +134,7 @@ bool PGOProfileData::getFunctionCounts(StringRef FuncName, uint64_t &FuncHash,
 
   for (unsigned N = 0; N < NumCounters; ++N) {
     // Read the count value.
-    uint64_t Count = strtoll(CurPtr, &EndPtr, 10);
+    uint64_t Count = strtoull(CurPtr, &EndPtr, 10);
     if (EndPtr == CurPtr || *EndPtr != '\n') {
       ReportBadPGOData(CGM, "pgo-data file has bad count value");
       return true;