]> diplodocus.org Git - nmh/commitdiff
mime_type(): Don't leak memory allocated by get_file_info().
authorRalph Corderoy <ralph@inputplus.co.uk>
Tue, 12 Sep 2017 12:20:56 +0000 (13:20 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Tue, 12 Sep 2017 13:17:27 +0000 (14:17 +0100)
Fixes b4f2851d4.

sbr/mime_type.c

index a640110..0bfc337 100644 (file)
@@ -33,17 +33,13 @@ mime_type(const char *file_name) {
         /* Try to append charset for text content. */
         char *mimeencoding;
 
-        if (strncasecmp(mimetype, "text", 4) == 0) {
-            if ((mimeencoding = get_file_info(MIMEENCODINGPROC, file_name))) {
-                content_type = concat(mimetype, "; charset=", mimeencoding,
-                                      NULL);
-                free (mimetype);
-            } else {
-                content_type = mimetype;
-            }
-        } else {
+        if (!strncasecmp(mimetype, "text", 4) &&
+            (mimeencoding = get_file_info(MIMEENCODINGPROC, file_name))) {
+            content_type = concat(mimetype, "; charset=", mimeencoding, NULL);
+            free(mimeencoding);
+            free(mimetype);
+        } else
             content_type = mimetype;
-        }
 #else  /* MIMEENCODINGPROC */
         content_type = mimetype;
 #endif /* MIMEENCODINGPROC */
@@ -112,7 +108,7 @@ mime_type(const char *file_name) {
 #ifdef MIMETYPEPROC
 /*
  * Get information using proc about a file.
- */
+ * Non-null return value must be free(3)'d. */
 static char *
 get_file_info(const char *proc, const char *file_name)
 {