lpereira / lwan

Experimental, scalable, high performance HTTP server
https://lwan.ws
GNU General Public License v2.0
5.92k stars 549 forks source link

Resource leak #323

Closed rtczza closed 2 years ago

rtczza commented 2 years ago

src/bin/tools/mimegen.c:223:9: error: Resource leak: fp [resourceLeak]

199     FILE *fp;
200     char buffer[256];
201     struct output output = { .capacity = 1024 };
202     size_t compressed_size;
203     char *compressed, *ext;
204     struct hash *ext_mime;
205     struct hash_iter iter;
206     const char **exts, *key;
207     size_t i;
208 
209     if (argc < 2) {
210         fprintf(stderr, "Usage: %s /path/to/mime.types\n", argv[0]);
211         return 1;
212     }
213 
214     fp = fopen(argv[1], "re");
215     if (!fp) {
216         fprintf(stderr, "Could not open %s: %s\n", argv[1], strerror(errno));
217         return 1;
218     }
219 
220     ext_mime = hash_str_new(free, free);
221     if (!ext_mime) {
222         fprintf(stderr, "Could not allocate hash table\n");
223         return 1;
224     }
225 

The following can correct the above error:

diff --git a/src/bin/tools/mimegen.c b/src/bin/tools/mimegen.c
index f1edacd..2185be9 100644
--- a/src/bin/tools/mimegen.c
+++ b/src/bin/tools/mimegen.c
@@ -220,6 +220,7 @@ int main(int argc, char *argv[])
     ext_mime = hash_str_new(free, free);
     if (!ext_mime) {
         fprintf(stderr, "Could not allocate hash table\n");
+        fclose(fp);
         return 1;
     }
HiSunzhenliang commented 2 years ago

More More possible memory leaks. Like:

...
    compressed = compress_output(&output, &compressed_size);
    if (!compressed) {
        fprintf(stderr, "Could not compress data\n");
        return 1;
    }
...

and so on.

lpereira commented 2 years ago

Please send a PR addressing these. However, leaks in mimegen aren't a problem because it's not a long-running program anyway; it would be fine it it exited without freeing memory or closing anything as well.

On Thu, Dec 16, 2021, 07:18 Alliswell @.***> wrote:

More More possible memory leaks. Like:

... compressed = compress_output(&output, &compressed_size); if (!compressed) { fprintf(stderr, "Could not compress data\n"); return 1; } ...

and so on.

— Reply to this email directly, view it on GitHub https://github.com/lpereira/lwan/issues/323#issuecomment-995912781, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAADVGM7QPKFLKYCOFILFQLURH7L3ANCNFSM5KFUTO3A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

lpereira commented 2 years ago

These have been plugged. Closing.