Thijs Alkemade avatar Thijs Alkemade committed a5e0d6a

Free correctly, and avoid malloc

Comments (0)

Files changed (1)

     strcat(path, "/.hg/branch");
     
     FILE *branch_file = fopen(path, "r");
+    free(path);
     
     if (!branch_file) {
+        printf("default");
         return;
     }
     
-    char *branch = malloc(sizeof(char) * 1024);
+    char branch[1024];
     
     int count = fread(branch, sizeof(char), 1024, branch_file);
     branch[count - 1] = 0;
     
     printf("%s", branch);
     
-    free(branch);
     fclose(branch_file);
 }
 
     strcat(path, "/.hg/dirstate");
     
     FILE *dirstate_file = fopen(path, "r");
+    free(path);
     
     if (!dirstate_file) {
         printf("000000");
         return;
     }
     
-    char *hash = malloc(sizeof(char) * 20);
+    char hash[20];
     
     fread(hash, sizeof(char), 20, dirstate_file);
     
         printf("%.2x", hash[i] & 0xff);
     }
     
-    free(hash);
     fclose(dirstate_file);
 }
 
     strcat(path, "/.hg/patches/series");
     FILE *series_file = fopen(path, "r");
     
+    free(path);
+    
+    if (!series_file) {
+        return;
+    }
+    
     char *status_path = strdup(root_path);
     strcat(status_path, "/.hg/patches/status");
     FILE *status_file = fopen(status_path, "r");
     
-    if (!series_file || !status_file) {
+    free(status_path);
+    
+    
+    if (!status_file) {
+        fclose(series_file);
         return;
     }
     
     printf(GREEN);
     
     int max = 1024;
-    char *status = (char *)malloc(sizeof(char) * (max + 1));
-    fgets(status, max, status_file);
+    // char *status = (char *)malloc(sizeof(char) * (max + 1));
+    char status[1024];
+    char *success = fgets(status, max, status_file);
     
-    while(status != NULL) {
+    while(success != NULL) {
         
         status[strlen(status) - 1] = 0;
-        status = strstr(status, ":");
         
-        if (status == NULL) break;
+        char *name = strstr(status, ":");
         
-        printf(YELLOW " → " GREEN "%s", status + sizeof(char));
+        if (!name) break;
         
-        status = fgets(status, max, status_file);
+        name++;
+        
+        printf(YELLOW " → " GREEN "%s", name);
+        
+        success = fgets(status, max, status_file);
     }
     
-    free(status);
-    
     printf(YELLOW);
     
     return;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.