check that malloc succeeds and use safe strcpy/strcat
authorWolf Vollprecht <w.vollprecht@gmail.com>
Sat, 8 Jan 2022 16:10:52 +0000 (17:10 +0100)
committerWolf Vollprecht <w.vollprecht@gmail.com>
Sat, 8 Jan 2022 16:10:52 +0000 (17:10 +0100)
test/shacheck.c

index 48f922fa6fb22a72131200d8cd1d70c250160a50..68a1446710e9ba8ef251ded4a2bb55538cef4f31 100644 (file)
@@ -68,11 +68,16 @@ int main (int argc, char *argv[]) {
     int status;
 #ifdef _WIN32
     char* fullcmd = malloc(2000);
-    strcpy(fullcmd, args[0]);
+    if (!fullcmd)
+    {
+        printf("Unable to allocate 2000 bytes\n");
+        exit(1);
+    }
+    strcpy_s(fullcmd, 2000, args[0]);
     for(int i=1; i<argc-3; i++)
     {
-        strcat(fullcmd, " ");
-        strcat(fullcmd, args[i]);
+        strcat_s(fullcmd, 2000, " ");
+        strcat_s(fullcmd, 2000, args[i]);
     }
     status = system(fullcmd);
     free(fullcmd);
@@ -105,6 +110,11 @@ int main (int argc, char *argv[]) {
     }
     /* Files must be smaller than 1MB  */
     char* data = malloc(1024*1024);
+    if (!data)
+    {
+        printf("Could not allocate 1024*1024 bytes for reading\n");
+        exit(1);
+    }
     ssize_t len = read(in, data, 1024*1024);
     if(len < 0) {
         perror("");