]> sigrok.org Git - sigrok-cli.git/commitdiff
input: minor robustness improvement, close fd leak
authorGerhard Sittig <redacted>
Sat, 23 Apr 2022 19:25:35 +0000 (21:25 +0200)
committerGerhard Sittig <redacted>
Sat, 23 Apr 2022 19:36:34 +0000 (21:36 +0200)
Eliminate redundant CHUNK_SIZE references, just fill the GString buffer
as far as it was allocated no matter how. Call an input related free
routine for options, not the output routine (which happened to work by
coincidence, thus went unnoticed). Close the input file descriptor after
the read loop, the caller has got no reference to it.

input.c

diff --git a/input.c b/input.c
index eb496ee7a54d4c2a806c248f282bbf05efb94178..eafbd6fdb8b0ee286f3db7c299f4445e78b79287 100644 (file)
--- a/input.c
+++ b/input.c
@@ -68,7 +68,7 @@ static void load_input_file_module(struct df_arg_desc *df_arg)
                if ((options = sr_input_options_get(imod))) {
                        mod_opts = generic_arg_to_opt(options, mod_args);
                        (void)warn_unknown_keys(options, mod_args, NULL);
-                       sr_output_options_free(options);
+                       sr_input_options_free(options);
                } else {
                        mod_opts = NULL;
                }
@@ -105,7 +105,7 @@ static void load_input_file_module(struct df_arg_desc *df_arg)
                                        g_critical("Failed to load %s: %s.", opt_input_file,
                                                        g_strerror(errno));
                        }
-                       if ((len = read(fd, buf->str, CHUNK_SIZE)) < 1)
+                       if ((len = read(fd, buf->str, buf->allocated_len)) < 1)
                                g_critical("Failed to read %s: %s.", opt_input_file,
                                                g_strerror(errno));
                        buf->len = len;
@@ -139,7 +139,7 @@ static void load_input_file_module(struct df_arg_desc *df_arg)
                if (push_scan_data)
                        len = 0;
                else
-                       len = read(fd, buf->str, CHUNK_SIZE);
+                       len = read(fd, buf->str, buf->allocated_len);
                if (len < 0)
                        g_critical("Read failed: %s", g_strerror(errno));
                if (len == 0 && !push_scan_data)
@@ -166,6 +166,7 @@ static void load_input_file_module(struct df_arg_desc *df_arg)
        sr_input_end(in);
        sr_input_free(in);
        g_string_free(buf, TRUE);
+       close(fd);
 
        df_arg->session = NULL;
        sr_session_destroy(session);