]> granicus.if.org Git - llvm/commitdiff
[Support] Treat truncation of fullpath as error
authorJonas Hahnfeld <hahnjo@hahnjo.de>
Wed, 13 Mar 2019 10:37:56 +0000 (10:37 +0000)
committerJonas Hahnfeld <hahnjo@hahnjo.de>
Wed, 13 Mar 2019 10:37:56 +0000 (10:37 +0000)
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

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

lib/Support/Unix/Path.inc

index 5eba86d2077d9da6d50dad4ed516c2564f600efb..57385baaf34bc98da158835bdc2448afde1417e4 100644 (file)
@@ -107,7 +107,11 @@ test_dir(char ret[PATH_MAX], const char *dir, const char *bin)
   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)