Fossil SCM
Fix another memory leak in the "fossil fusefs" command.
Commit
6b150197653b89e7d6ade1d1084cb98797da4a8e
Parent
7a7ef00b3500bb9…
1 file changed
+28
-12
+28
-12
| --- src/fusefs.c | ||
| +++ src/fusefs.c | ||
| @@ -49,23 +49,31 @@ | ||
| 49 | 49 | ManifestFile *pFile; /* Name of a cached file */ |
| 50 | 50 | Blob content; /* Content of the cached file */ |
| 51 | 51 | /* Parsed path */ |
| 52 | 52 | char *az[3]; /* 0=type, 1=id, 2=path */ |
| 53 | 53 | } fusefs; |
| 54 | + | |
| 55 | +/* | |
| 56 | +** Clear the fusefs.sz[] array. | |
| 57 | +*/ | |
| 58 | +static void fusefs_clear_path(void){ | |
| 59 | + int i; | |
| 60 | + for(i=0; i<count(fusefs.az); i++){ | |
| 61 | + fossil_free(fusefs.az[i]); | |
| 62 | + fusefs.az[i] = 0; | |
| 63 | + } | |
| 64 | +} | |
| 54 | 65 | |
| 55 | 66 | /* |
| 56 | 67 | ** Split of the input path into 0, 1, 2, or 3 elements in fusefs.az[]. |
| 57 | 68 | ** Return the number of elements. |
| 58 | 69 | ** |
| 59 | 70 | ** Any prior path parse is deleted. |
| 60 | 71 | */ |
| 61 | 72 | static int fusefs_parse_path(const char *zPath){ |
| 62 | 73 | int i, j; |
| 63 | - for(i=0; i<count(fusefs.az); i++){ | |
| 64 | - fossil_free(fusefs.az[i]); | |
| 65 | - fusefs.az[i] = 0; | |
| 66 | - } | |
| 74 | + fusefs_clear_path(); | |
| 67 | 75 | if( strcmp(zPath,"/")==0 ) return 0; |
| 68 | 76 | for(i=0, j=1; i<2 && zPath[j]; i++){ |
| 69 | 77 | int jStart = j; |
| 70 | 78 | while( zPath[j] && zPath[j]!='/' ){ j++; } |
| 71 | 79 | fusefs.az[i] = mprintf("%.*s", j-jStart, &zPath[jStart]); |
| @@ -72,21 +80,30 @@ | ||
| 72 | 80 | if( zPath[j] ) j++; |
| 73 | 81 | } |
| 74 | 82 | if( zPath[j] ) fusefs.az[i++] = fossil_strdup(&zPath[j]); |
| 75 | 83 | return i; |
| 76 | 84 | } |
| 85 | + | |
| 86 | +/* | |
| 87 | +** Reclaim memory used by the fusefs local variable. | |
| 88 | +*/ | |
| 89 | +static void fusefs_reset(void){ | |
| 90 | + blob_reset(&fusefs.content); | |
| 91 | + manifest_destroy(fusefs.pMan); | |
| 92 | + fusefs.pMan = 0; | |
| 93 | + fossil_free(fusefs.zSymName); | |
| 94 | + fusefs.zSymName = 0; | |
| 95 | + fusefs.pFile = 0; | |
| 96 | +} | |
| 77 | 97 | |
| 78 | 98 | /* |
| 79 | 99 | ** Load manifest rid into the cache. |
| 80 | 100 | */ |
| 81 | 101 | static void fusefs_load_rid(int rid, const char *zSymName){ |
| 82 | 102 | if( fusefs.rid==rid && fusefs.pMan!=0 ) return; |
| 83 | - blob_reset(&fusefs.content); | |
| 84 | - manifest_destroy(fusefs.pMan); | |
| 85 | - fossil_free(fusefs.zSymName); | |
| 103 | + fusefs_reset(); | |
| 86 | 104 | fusefs.zSymName = fossil_strdup(zSymName); |
| 87 | - fusefs.pFile = 0; | |
| 88 | 105 | fusefs.pMan = manifest_get(rid, CFTYPE_MANIFEST, 0); |
| 89 | 106 | fusefs.rid = rid; |
| 90 | 107 | } |
| 91 | 108 | |
| 92 | 109 | /* |
| @@ -216,10 +233,11 @@ | ||
| 216 | 233 | } |
| 217 | 234 | cnt++; |
| 218 | 235 | } |
| 219 | 236 | pFile = manifest_file_next(fusefs.pMan, 0); |
| 220 | 237 | } |
| 238 | + fossil_free(zBase); | |
| 221 | 239 | } |
| 222 | 240 | return cnt>0 ? 0 : -ENOENT; |
| 223 | 241 | } |
| 224 | 242 | |
| 225 | 243 | |
| @@ -315,11 +333,9 @@ | ||
| 315 | 333 | azNewArgv[2] = "-s"; |
| 316 | 334 | azNewArgv[3] = zMountPoint; |
| 317 | 335 | azNewArgv[4] = 0; |
| 318 | 336 | g.localOpen = 0; /* Prevent tags like "current" and "prev" */ |
| 319 | 337 | fuse_main(4, azNewArgv, &fusefs_methods, NULL); |
| 320 | - manifest_destroy(fusefs.pMan); | |
| 321 | - blob_reset(&fusefs.content); | |
| 322 | - for(i=0; i<count(fusefs.az); i++) fossil_free(fusefs.az[i]); | |
| 323 | - memset(&fusefs, 0, sizeof(fusefs)); | |
| 338 | + fusefs_reset(); | |
| 339 | + fusefs_clear_path(); | |
| 324 | 340 | #endif |
| 325 | 341 | } |
| 326 | 342 |
| --- src/fusefs.c | |
| +++ src/fusefs.c | |
| @@ -49,23 +49,31 @@ | |
| 49 | ManifestFile *pFile; /* Name of a cached file */ |
| 50 | Blob content; /* Content of the cached file */ |
| 51 | /* Parsed path */ |
| 52 | char *az[3]; /* 0=type, 1=id, 2=path */ |
| 53 | } fusefs; |
| 54 | |
| 55 | /* |
| 56 | ** Split of the input path into 0, 1, 2, or 3 elements in fusefs.az[]. |
| 57 | ** Return the number of elements. |
| 58 | ** |
| 59 | ** Any prior path parse is deleted. |
| 60 | */ |
| 61 | static int fusefs_parse_path(const char *zPath){ |
| 62 | int i, j; |
| 63 | for(i=0; i<count(fusefs.az); i++){ |
| 64 | fossil_free(fusefs.az[i]); |
| 65 | fusefs.az[i] = 0; |
| 66 | } |
| 67 | if( strcmp(zPath,"/")==0 ) return 0; |
| 68 | for(i=0, j=1; i<2 && zPath[j]; i++){ |
| 69 | int jStart = j; |
| 70 | while( zPath[j] && zPath[j]!='/' ){ j++; } |
| 71 | fusefs.az[i] = mprintf("%.*s", j-jStart, &zPath[jStart]); |
| @@ -72,21 +80,30 @@ | |
| 72 | if( zPath[j] ) j++; |
| 73 | } |
| 74 | if( zPath[j] ) fusefs.az[i++] = fossil_strdup(&zPath[j]); |
| 75 | return i; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | ** Load manifest rid into the cache. |
| 80 | */ |
| 81 | static void fusefs_load_rid(int rid, const char *zSymName){ |
| 82 | if( fusefs.rid==rid && fusefs.pMan!=0 ) return; |
| 83 | blob_reset(&fusefs.content); |
| 84 | manifest_destroy(fusefs.pMan); |
| 85 | fossil_free(fusefs.zSymName); |
| 86 | fusefs.zSymName = fossil_strdup(zSymName); |
| 87 | fusefs.pFile = 0; |
| 88 | fusefs.pMan = manifest_get(rid, CFTYPE_MANIFEST, 0); |
| 89 | fusefs.rid = rid; |
| 90 | } |
| 91 | |
| 92 | /* |
| @@ -216,10 +233,11 @@ | |
| 216 | } |
| 217 | cnt++; |
| 218 | } |
| 219 | pFile = manifest_file_next(fusefs.pMan, 0); |
| 220 | } |
| 221 | } |
| 222 | return cnt>0 ? 0 : -ENOENT; |
| 223 | } |
| 224 | |
| 225 | |
| @@ -315,11 +333,9 @@ | |
| 315 | azNewArgv[2] = "-s"; |
| 316 | azNewArgv[3] = zMountPoint; |
| 317 | azNewArgv[4] = 0; |
| 318 | g.localOpen = 0; /* Prevent tags like "current" and "prev" */ |
| 319 | fuse_main(4, azNewArgv, &fusefs_methods, NULL); |
| 320 | manifest_destroy(fusefs.pMan); |
| 321 | blob_reset(&fusefs.content); |
| 322 | for(i=0; i<count(fusefs.az); i++) fossil_free(fusefs.az[i]); |
| 323 | memset(&fusefs, 0, sizeof(fusefs)); |
| 324 | #endif |
| 325 | } |
| 326 |
| --- src/fusefs.c | |
| +++ src/fusefs.c | |
| @@ -49,23 +49,31 @@ | |
| 49 | ManifestFile *pFile; /* Name of a cached file */ |
| 50 | Blob content; /* Content of the cached file */ |
| 51 | /* Parsed path */ |
| 52 | char *az[3]; /* 0=type, 1=id, 2=path */ |
| 53 | } fusefs; |
| 54 | |
| 55 | /* |
| 56 | ** Clear the fusefs.sz[] array. |
| 57 | */ |
| 58 | static void fusefs_clear_path(void){ |
| 59 | int i; |
| 60 | for(i=0; i<count(fusefs.az); i++){ |
| 61 | fossil_free(fusefs.az[i]); |
| 62 | fusefs.az[i] = 0; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | ** Split of the input path into 0, 1, 2, or 3 elements in fusefs.az[]. |
| 68 | ** Return the number of elements. |
| 69 | ** |
| 70 | ** Any prior path parse is deleted. |
| 71 | */ |
| 72 | static int fusefs_parse_path(const char *zPath){ |
| 73 | int i, j; |
| 74 | fusefs_clear_path(); |
| 75 | if( strcmp(zPath,"/")==0 ) return 0; |
| 76 | for(i=0, j=1; i<2 && zPath[j]; i++){ |
| 77 | int jStart = j; |
| 78 | while( zPath[j] && zPath[j]!='/' ){ j++; } |
| 79 | fusefs.az[i] = mprintf("%.*s", j-jStart, &zPath[jStart]); |
| @@ -72,21 +80,30 @@ | |
| 80 | if( zPath[j] ) j++; |
| 81 | } |
| 82 | if( zPath[j] ) fusefs.az[i++] = fossil_strdup(&zPath[j]); |
| 83 | return i; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | ** Reclaim memory used by the fusefs local variable. |
| 88 | */ |
| 89 | static void fusefs_reset(void){ |
| 90 | blob_reset(&fusefs.content); |
| 91 | manifest_destroy(fusefs.pMan); |
| 92 | fusefs.pMan = 0; |
| 93 | fossil_free(fusefs.zSymName); |
| 94 | fusefs.zSymName = 0; |
| 95 | fusefs.pFile = 0; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | ** Load manifest rid into the cache. |
| 100 | */ |
| 101 | static void fusefs_load_rid(int rid, const char *zSymName){ |
| 102 | if( fusefs.rid==rid && fusefs.pMan!=0 ) return; |
| 103 | fusefs_reset(); |
| 104 | fusefs.zSymName = fossil_strdup(zSymName); |
| 105 | fusefs.pMan = manifest_get(rid, CFTYPE_MANIFEST, 0); |
| 106 | fusefs.rid = rid; |
| 107 | } |
| 108 | |
| 109 | /* |
| @@ -216,10 +233,11 @@ | |
| 233 | } |
| 234 | cnt++; |
| 235 | } |
| 236 | pFile = manifest_file_next(fusefs.pMan, 0); |
| 237 | } |
| 238 | fossil_free(zBase); |
| 239 | } |
| 240 | return cnt>0 ? 0 : -ENOENT; |
| 241 | } |
| 242 | |
| 243 | |
| @@ -315,11 +333,9 @@ | |
| 333 | azNewArgv[2] = "-s"; |
| 334 | azNewArgv[3] = zMountPoint; |
| 335 | azNewArgv[4] = 0; |
| 336 | g.localOpen = 0; /* Prevent tags like "current" and "prev" */ |
| 337 | fuse_main(4, azNewArgv, &fusefs_methods, NULL); |
| 338 | fusefs_reset(); |
| 339 | fusefs_clear_path(); |
| 340 | #endif |
| 341 | } |
| 342 |