Here's a complete and relatively non-invasive change.
In addition to the API changes, it avoids a low-memory NULL deref.

>From 1dfcfbbb82697254fca47b82d114ef0dd98a23c2 Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email protected]>
Date: Mon, 6 Apr 2009 12:46:52 +0200
Subject: [PATCH] logsys.h (logsys_format_set): Change return type, adjust param 
type.

* include/corosync/engine/logsys.h:
* exec/logsys.c (logsys_format_set): Return -1 upon strdup failure.
Change type of param to "const char *".
* exec/logsys.c (logsys_init): Adjust use.
* exec/mainconfig.c (corosync_main_config_read_logging): Adjust uses.
---
 exec/logsys.c                    |    9 +++++++--
 exec/mainconfig.c                |   17 +++++++++++------
 include/corosync/engine/logsys.h |    6 +++---
 3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/exec/logsys.c b/exec/logsys.c
index f122ff2..1b83fa7 100644
--- a/exec/logsys.c
+++ b/exec/logsys.c
@@ -916,7 +916,7 @@ int logsys_config_file_set (const char **error_string, 
const char *file)
        return (0);
 }

-void logsys_format_set (char *format)
+int logsys_format_set (const char *format)
 {
        pthread_mutex_lock (&logsys_config_mutex);

@@ -930,8 +930,12 @@ void logsys_format_set (char *format)
        } else {
                format_buffer = strdup("[%6s] %b");
        }
+       if (format_buffer == NULL) {
+               return -1;
+       }

        pthread_mutex_unlock (&logsys_config_mutex);
+       return 0;
 }

 char *logsys_format_get (void)
@@ -1137,7 +1141,8 @@ int logsys_init (
        logsys_config_mode_set (mode);
        logsys_config_facility_set (name, facility);
        logsys_config_file_set (&errstr, file);
-       logsys_format_set (format);
+       if (logsys_format_set (format))
+               return -1;
        _logsys_rec_init (rec_size);
        _logsys_wthread_create ();
        return (0);
diff --git a/exec/mainconfig.c b/exec/mainconfig.c
index b8dc0d5..7dc7a28 100644
--- a/exec/mainconfig.c
+++ b/exec/mainconfig.c
@@ -195,6 +195,7 @@ int corosync_main_config_read_logging (
        hdb_handle_t object_find_handle;
        hdb_handle_t object_find_logsys_handle;
        char new_format_buffer[PATH_MAX];
+       int err = 0;

        objdb->object_find_create (
                OBJECT_PARENT_HANDLE,
@@ -236,12 +237,12 @@ int corosync_main_config_read_logging (
                                if (!insert_into_buffer(new_format_buffer,
                                                sizeof(new_format_buffer),
                                                " %f:%l", "s]")) {
-                                       logsys_format_set(new_format_buffer);
+                                       err = 
logsys_format_set(new_format_buffer);
                                } else
                                if (!insert_into_buffer(new_format_buffer,
                                                sizeof(new_format_buffer),
                                                "%f:%l", NULL)) {
-                                       logsys_format_set(new_format_buffer);
+                                       err = 
logsys_format_set(new_format_buffer);
                                }
                        } else
                        if (strcmp (value, "off") == 0) {
@@ -255,12 +256,12 @@ int corosync_main_config_read_logging (
                                if (!insert_into_buffer(new_format_buffer,
                                                sizeof(new_format_buffer),
                                                "%n:", "f:")) {
-                                       logsys_format_set(new_format_buffer);
+                                       err = 
logsys_format_set(new_format_buffer);
                                } else
                                if (!insert_into_buffer(new_format_buffer,
                                                sizeof(new_format_buffer),
                                                " %n", "s]")) {
-                                       logsys_format_set(new_format_buffer);
+                                       err = 
logsys_format_set(new_format_buffer);
                                }
                        } else
                        if (strcmp (value, "off") == 0) {
@@ -274,7 +275,7 @@ int corosync_main_config_read_logging (
                                if(!insert_into_buffer(new_format_buffer,
                                                sizeof(new_format_buffer),
                                                "%t ", NULL)) {
-                                       logsys_format_set(new_format_buffer);
+                                       err = 
logsys_format_set(new_format_buffer);
                                }
                        } else
                        if (strcmp (value, "off") == 0) {
@@ -283,6 +284,10 @@ int corosync_main_config_read_logging (
                                goto parse_error;
                        }
                }
+               if (err) {
+                       error_reason = "exhausted virtual memory";
+                       goto parse_error;
+               }

                /* free old string on reload */
                if (main_config->logfile) {
@@ -365,7 +370,7 @@ int corosync_main_config_read_logging (
                                logsys_logger.subsys,
                                logsys_logger.tags,
                                logsys_logger.priority);
-                       
+
                }
                objdb->object_find_destroy (object_find_logsys_handle);
        }
diff --git a/include/corosync/engine/logsys.h b/include/corosync/engine/logsys.h
index 25fc59b..36e30e4 100644
--- a/include/corosync/engine/logsys.h
+++ b/include/corosync/engine/logsys.h
@@ -94,8 +94,8 @@ extern void logsys_config_facility_set (
        const char *name,
        unsigned int facility);

-extern void logsys_format_set (
-       char *format);
+extern int logsys_format_set (
+       const char *format);

 extern char *logsys_format_get (void);

@@ -182,7 +182,7 @@ __attribute__ ((constructor)) static void 
logsys_system_init (void) \
        logsys_config_mode_set (mode);                                  \
        logsys_config_file_set (&error_string, (file));                 \
        logsys_config_facility_set (name, (facility));                  \
-       logsys_format_set (format);                                     \
+       logsys_format_set (format); /* FIXME: assert success? */        \
        _logsys_rec_init (rec_size);                                    \
        _logsys_wthread_create();                                       \
 }
-- 
1.6.2.rc1.285.gc5f54
_______________________________________________
Openais mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais

Reply via email to