Hello All,

    In reviewing source code in linuxptp-1.5, I found several instances
where the return values for calls to malloc()/calloc()/strdup() are not
checked for a return value of NULL, indicating failure.  This is in file
'timemaster.c', and the patch file below should address these issues,
and also free()'s previously allocated memory in the order it was
allocated:

=======================================================================

--- timemaster.c.orig    2015-08-19 18:35:03.044000000 -0700
+++ timemaster.c    2015-08-19 18:57:01.046000000 -0700
@@ -268,9 +268,18 @@
     }

     source = malloc(sizeof(*source));
+    if (!source) {
+        pr_err("low memory, failed to add source to ntp_server");
+        return NULL;
+    }
     source->type = NTP_SERVER;
     source->ntp = ntp_server;
     source->ntp.address = strdup(source->ntp.address);
+    if (!source->ntp.address) {
+        pr_err("low memory, failed to add source->ntp.address");
+        free(source);
+        return NULL;
+    }

     return source;
 }
@@ -282,6 +291,10 @@
     int r = 0;

     source = malloc(sizeof(*source));
+    if (!source) {
+        pr_err("low memory, failed to add source to ptp_parse");
+        goto failed;
+    }
     source->type = PTP_DOMAIN;
     source->ptp.delay = DEFAULT_PTP_DELAY;
     source->ptp.ntp_poll = DEFAULT_PTP_NTP_POLL;
@@ -482,10 +495,19 @@
     char buf[4096], *line, *section_name = NULL;
     char **section_lines = NULL;
     int ret = 0;
-
+
+    if (!config) {
+        pr_err("low memory, failed to add config to config_parse");
+        return NULL;
+    }
     config->sources = (struct source **)parray_new();
     config->ntp_program = DEFAULT_NTP_PROGRAM;
     config->rundir = strdup(DEFAULT_RUNDIR);
+    if (!config->rundir) {
+        pr_err("low memory, failed to add config->rundir to config_parse");
+        free(config);
+        return NULL;
+    }

     init_program_config(&config->chronyd, "chronyd",
                 NULL, DEFAULT_CHRONYD_SETTINGS, NULL);
@@ -666,6 +688,10 @@

     /* get PHCs used by specified interfaces */
     phcs = malloc(num_interfaces * sizeof(int));
+    if (!phcs) {
+        pr_err("low memory, failed to add phcs to ptp_source");
+        return 0;
+    }
     for (i = 0; i < num_interfaces; i++) {
         phcs[i] = -1;

@@ -719,6 +745,11 @@

             /* don't use this PHC in other sources */
             phc = malloc(sizeof(int));
+            if (!phc) {
+                pr_err("low memory, failed to add phc to ptp_source");
+                free(phcs);
+                return 0;
+            }
             *phc = phcs[i];
             parray_append((void ***)allocated_phcs, phc);
         }
@@ -727,9 +758,22 @@
                        config->rundir, *shm_segment);

         config_file = malloc(sizeof(*config_file));
+        if (!config_file) {
+            pr_err("low memory, failed to add config_file to ptp_source");
+            free(phc);
+            free(phcs);
+            return 0;
+        }
         config_file->path = string_newf("%s/ptp4l.%d.conf",
                         config->rundir, *shm_segment);
         config_file->content = strdup("[global]\n");
+        if (!config_file->content) {
+            pr_err("low memory, failed to add config_file->content to
ptp_source");
+            free(config_file);
+            free(phc);
+            free(phcs);
+            return 0;
+        }
         extend_config_string(&config_file->content,
                      config->ptp4l.settings);
         extend_config_string(&config_file->content,
@@ -811,7 +855,17 @@
     struct config_file *ntp_config = malloc(sizeof(*ntp_config));
     char **command = NULL;

+    if (!ntp_config) {
+        pr_err("low memory, failed to add ntp_config to add_ntp_program");
+        return NULL;
+    }
+
     ntp_config->content = strdup("");
+    if (!ntp_config->content) {
+        pr_err("low memory, failed to add ntp_config->content to
add_ntp_program");
+        free(ntp_config);
+        return NULL;
+    }

     switch (config->ntp_program) {
     case CHRONYD:
@@ -866,6 +920,10 @@
     int **allocated_phcs = (int **)parray_new();
     int ret = 0, shm_segment = 0;

+    if (!script) {
+        pr_err("low memory, failed to add script to script_create");
+        return NULL;
+    }
     script->configs = (struct config_file **)parray_new();
     script->commands = (char ***)parray_new();

=======================================================================

running 'make' in the linuxptp-1.5 source tree (with the code added
above) results in a clean build, btw.

I am attaching the patch file (diff -u format) to this bug report...

Comments, Questions, Complaints, Suggestions? :)

Bill Parker (wp02855 at gmail dot com)

Attachment: timemaster.c.patch
Description: Binary data

------------------------------------------------------------------------------
_______________________________________________
Linuxptp-users mailing list
Linuxptp-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-users

Reply via email to