]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Refactor fgets usage, fix resource leaks
authorShwetha K Acharya <Shwetha.K.Acharya@ibm.com>
Tue, 20 May 2025 07:59:59 +0000 (13:29 +0530)
committerVolker Lendecke <vl@samba.org>
Fri, 13 Jun 2025 16:46:33 +0000 (16:46 +0000)
This commit refactors the code by replacing fgets with
getline for more reliable line reading.

Additionally this PR,
- Corrects the error message for calloc failure.
- Ensures that resources are properly freed and
  closed before calling errx(), fixing potential
  resource leaks.
- Adds a check to gracefully skip newlines and
  comment lines(starting with #)
- simplifies error handling by grouping repetitive
  free statements under goto block

Signed-off-by: Shwetha K Acharya <Shwetha.K.Acharya@ibm.com>
Reviewed-by: Anoop C S <anoopcs@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Fri Jun 13 16:46:33 UTC 2025 on atb-devel-224

lib/texpect/texpect.c

index 1a6ebf486ae687223a4a873b05668136089b8004..d0603db3e0cd35bd3863e887158e6d35e0eeda85 100644 (file)
@@ -183,42 +183,62 @@ static char *iscmd(const char *buf, const char *s)
 static void parse_configuration(const char *fn)
 {
        struct command *c;
-       char s[1024];
+       char *conf_line = NULL;
        char *str;
+       size_t len = 0;
        unsigned int lineno = 0;
        FILE *cmd;
+       const char *err_message = NULL;
+       char err_buf[256];
 
        cmd = fopen(fn, "r");
        if (cmd == NULL)
                err(1, "open: %s", fn);
 
-       while (fgets(s, sizeof(s),  cmd) != NULL) {
-
-               s[strcspn(s, "#\n")] = '\0';
+       while (getline(&conf_line, &len, cmd) != -1) {
                lineno++;
 
+               conf_line[strcspn(conf_line, "#\n")] = '\0';
+               if (conf_line[0] == '\0') {
+                       continue;
+               }
+
                c = calloc(1, sizeof(*c));
-               if (c == NULL)
-                       errx(1, "malloc");
+               if (c == NULL) {
+                       err_message = "calloc failed";
+                       goto out;
+               }
 
                c->lineno = lineno;
-               (*next) = c;
-               next = &(c->next);
 
-               if ((str = iscmd(s, "expect ")) != NULL) {
+               if ((str = iscmd(conf_line, "expect ")) != NULL) {
                        c->type = CMD_EXPECT;
-                       c->str = str;
-               } else if ((str = iscmd(s, "send ")) != NULL) {
+               } else if ((str = iscmd(conf_line, "send ")) != NULL) {
                        c->type = CMD_SEND;
-                       c->str = str;
-               } else if ((str = iscmd(s, "password ")) != NULL) {
+               } else if ((str = iscmd(conf_line, "password ")) != NULL) {
                        c->type = CMD_PASSWORD;
-                       c->str = str;
-               } else
-                       errx(1, "Invalid command on line %d: %s", lineno, s);
-       }
+               } else {
+                       free(c);
+                       snprintf(err_buf,
+                                sizeof(err_buf),
+                                "Invalid command on line %d: %s",
+                                lineno,
+                                conf_line);
+                       err_message = err_buf;
+                       goto out;
+               }
 
+               c->str = str;
+
+               (*next) = c;
+               next = &(c->next);
+       }
+out:
+       free(conf_line);
        fclose(cmd);
+       if (err_message) {
+               errx(1, "%s", err_message);
+       }
 }
 
 /*