summarylogtreecommitdiffstats
path: root/inode-wipe-overflow-fix.patch
blob: b51bdc8e14da162177f231763b16b8f1f599c0b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--- sdel-lib.c.orig	2026-02-02 20:28:42.426954714 +0700
+++ sdel-lib.c	2026-02-02 20:30:20.645079731 +0700
@@ -307,7 +307,10 @@
 }
 
 void sdel_wipe_inodes(char *loc, char **array) {
-    char *template = malloc(strlen(loc) + 16);
+    const char *pat = "xxxxxxxx.xxx";
+    size_t loclen = strlen(loc);
+    int need_slash = (loclen > 0 && loc[loclen - 1] != '/');
+    char *template = malloc(loclen + need_slash + strlen(pat) + 1);
     int i = 0;
     int fail = 0;
     int fd;
@@ -315,25 +318,29 @@
     if (verbose)
         printf("Wiping inodes ...");
 
-    array = malloc(MAXINODEWIPE * sizeof(template));
+    array = malloc((MAXINODEWIPE + 1) * sizeof(*array));
+    if (!array || !template) { perror("malloc"); exit(1); }
     strcpy(template, loc);
-    if (loc[strlen(loc) - 1] != '/')
+    if (need_slash)
         strcat(template, "/");
-    strcat(template, "xxxxxxxx.xxx");
+    strcat(template, pat);
        
     while(i < MAXINODEWIPE && fail < 5) {
         __sdel_random_filename(template);
-        if (open(template, O_CREAT | O_EXCL | O_WRONLY, 0600) < 0)
+        fd = open(template, O_CREAT | O_EXCL | O_WRONLY, 0600);
+        if (fd < 0)
             fail++;
         else {
-            array[i] = malloc(strlen(template));
+            (void) close(fd);
+            array[i] = malloc(strlen(template) + 1);
             strcpy(array[i], template);
             i++;
         }
     }
+    free(template);
     FLUSH;
        
-    if (fail < 5) {
+    if (fail >= 5) {
         fprintf(stderr, "Warning: could not wipe all inodes!\n");
     }