[PATCH] D58835: [Support] Treat truncation of fullpath as error

Jonas Hahnfeld via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 1 10:26:18 PST 2019


Hahnfeld created this revision.
Hahnfeld added reviewers: sylvestre.ledru, chandlerc, hans, eli.friedman.
Herald added subscribers: llvm-commits, jdoerfert, kristina, hiraditya.
Herald added a project: LLVM.

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.)


Repository:
  rL LLVM

https://reviews.llvm.org/D58835

Files:
  llvm/lib/Support/Unix/Path.inc


Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -107,7 +107,11 @@
   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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58835.188938.patch
Type: text/x-patch
Size: 622 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190301/5fc2002c/attachment.bin>


More information about the llvm-commits mailing list