Fix an out of bounds array access.
authorRafael Espindola <rafael.espindola@gmail.com>
Mon, 10 Jun 2013 15:22:18 +0000 (15:22 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Mon, 10 Jun 2013 15:22:18 +0000 (15:22 +0000)
We were looking at Magic[5] without checking Length. Since this path would not
return unless Length >= 18 anyway, just move the >= 18 check up.

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

lib/Support/Path.cpp

index 5b34c5eab6843ce0c8ec3a1d68e0e738393e48c9..b6eeb1484cfd981fdb9798b92ba7c219e52fb4d6 100644 (file)
@@ -58,11 +58,12 @@ sys::identifyFileType(const char *Magic, unsigned Length) {
       break;
 
     case '\177':
-      if (Magic[1] == 'E' && Magic[2] == 'L' && Magic[3] == 'F') {
+      if (Length >= 18 && Magic[1] == 'E' && Magic[2] == 'L' &&
+          Magic[3] == 'F') {
         bool Data2MSB = Magic[5] == 2;
         unsigned high = Data2MSB ? 16 : 17;
         unsigned low  = Data2MSB ? 17 : 16;
-        if (Length >= 18 && Magic[high] == 0)
+        if (Magic[high] == 0)
           switch (Magic[low]) {
             default: break;
             case 1: return ELF_Relocatable_FileType;