]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
fix memory leak config read
authorBastian Dehn <hhaalo@arcor.de>
Sun, 3 Mar 2024 08:56:49 +0000 (09:56 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 3 Mar 2024 08:56:49 +0000 (09:56 +0100)
src/kds_s2000w_config.c

index 79c0d2454be38a2dd2380695b2809fa8cc3e67d5..1894eb0243f3cfc22750f45740e87732b0a4e280 100644 (file)
@@ -5,8 +5,6 @@
 
 char* read_config_file(const char* filename)
 {
-       char* config_stream = NULL;
-
        FILE* config_file = fopen(filename, "r");
 
        int char_count = 0;
@@ -14,10 +12,13 @@ char* read_config_file(const char* filename)
        while(fread(buf, sizeof(char), 1, config_file)) {
                char_count++;
        }
+       free(buf);
+       buf = NULL;
        fseek(config_file, 0L, SEEK_SET);
 
-       config_stream = malloc(sizeof(char) * char_count);
+       char* config_stream = malloc(sizeof(char) * char_count);
        fread(config_stream, char_count, 1, config_file);
+       config_stream[char_count - 1 ] = '\0';
        fclose(config_file);
        config_file = NULL;