[llvm] r356036 - [Support] Treat truncation of fullpath as error

Jonas Hahnfeld via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 13 03:37:56 PDT 2019


Author: hahnfeld
Date: Wed Mar 13 03:37:56 2019
New Revision: 356036

URL: http://llvm.org/viewvc/llvm-project?rev=356036&view=rev
Log:
[Support] Treat truncation of fullpath as error

If the concatenation of arguments dir and bin has at least PATH_MAX
characters the call to snprintf will truncate. The result will usually
not exist, but if it does it's actually incorrect to return that the
path exists.
(Motivated by GCC compiler warning about format truncation.)

Differential Revision: https://reviews.llvm.org/D58835

Modified:
    llvm/trunk/lib/Support/Unix/Path.inc

Modified: llvm/trunk/lib/Support/Unix/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Path.inc?rev=356036&r1=356035&r2=356036&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc (original)
+++ llvm/trunk/lib/Support/Unix/Path.inc Wed Mar 13 03:37:56 2019
@@ -107,7 +107,11 @@ test_dir(char ret[PATH_MAX], const char
   struct stat sb;
   char fullpath[PATH_MAX];
 
-  snprintf(fullpath, PATH_MAX, "%s/%s", dir, bin);
+  int chars = snprintf(fullpath, PATH_MAX, "%s/%s", dir, bin);
+  // We cannot write PATH_MAX characters because the string will be terminated
+  // with a null character. Fail if truncation happened.
+  if (chars >= PATH_MAX)
+    return 1;
   if (!realpath(fullpath, ret))
     return 1;
   if (stat(fullpath, &sb) != 0)




More information about the llvm-commits mailing list