[PATCH] D16582: fix array index out of bounds

Daniel Marjamäki via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 26 07:25:17 PST 2016

danielmarjamaki created this revision.
danielmarjamaki added a subscriber: cfe-commits.
danielmarjamaki set the repository for this revision to rL LLVM.

This little patch fixes possible array index out of bounds:

[tools/clang/lib/Driver/MSVCToolChain.cpp:147]: (error) Array 'partialKey[256]' accessed at index 256, which is out of bounds

There is a slight change in behaviour now. If partialKey is filled with data then partialData[255] is zeroed instead of partialData[256]. I thought about making partialData one byte bigger so partialData[256]='\0' would be ok but I _think_ that it is 256 elements by intention.




Index: lib/Driver/MSVCToolChain.cpp
--- lib/Driver/MSVCToolChain.cpp
+++ lib/Driver/MSVCToolChain.cpp
@@ -141,8 +141,8 @@
     size_t partialKeyLength = keyEnd - keyPath;
     char partialKey[256];
-    if (partialKeyLength > sizeof(partialKey))
-      partialKeyLength = sizeof(partialKey);
+    if (partialKeyLength >= sizeof(partialKey))
+      partialKeyLength = sizeof(partialKey) - 1;
     strncpy(partialKey, keyPath, partialKeyLength);
     partialKey[partialKeyLength] = '\0';
     HKEY hTopKey = NULL;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16582.45991.patch
Type: text/x-patch
Size: 598 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160126/61ad786b/attachment.bin>

More information about the cfe-commits mailing list