Skip to content

Commit d3c6756

Browse files
committed
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@356036 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8ab0c06 commit d3c6756

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/Support/Unix/Path.inc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ test_dir(char ret[PATH_MAX], const char *dir, const char *bin)
107107
struct stat sb;
108108
char fullpath[PATH_MAX];
109109

110-
snprintf(fullpath, PATH_MAX, "%s/%s", dir, bin);
110+
int chars = snprintf(fullpath, PATH_MAX, "%s/%s", dir, bin);
111+
// We cannot write PATH_MAX characters because the string will be terminated
112+
// with a null character. Fail if truncation happened.
113+
if (chars >= PATH_MAX)
114+
return 1;
111115
if (!realpath(fullpath, ret))
112116
return 1;
113117
if (stat(fullpath, &sb) != 0)

0 commit comments

Comments
 (0)