cfg: Terminate cfg to prevent crashes
authorJustin Pettit <jpettit@nicira.com>
Sat, 1 Aug 2009 07:58:31 +0000 (00:58 -0700)
committerJustin Pettit <jpettit@nicira.com>
Sat, 1 Aug 2009 07:58:31 +0000 (00:58 -0700)
If cfg_* accessor calls were made before cfg_read() was called (or it
returned error), they could cause segfault.  This checkin terminates the
cfg structure in such a way that will prevent these run-time problems.

Bug #1693

lib/cfg.c
lib/cfg.h

index 433d7a0..225827e 100644 (file)
--- a/lib/cfg.c
+++ b/lib/cfg.c
@@ -92,6 +92,12 @@ static bool is_type(const char *s, enum cfg_flags);
 #define CC_FILE_NAME CC_ALNUM "._-"
 #define CC_KEY CC_ALNUM "._-@$:+"
 
+void
+cfg_init(void)
+{
+    svec_terminate(&cfg);
+}
+
 /* Sets 'file_name' as the configuration file read by cfg_read().  Returns 0 on
  * success, otherwise a positive errno value if 'file_name' cannot be opened.
  *
@@ -183,6 +189,7 @@ cfg_read(void)
     file = fopen(cfg_name, "r");
     if (!file) {
         VLOG_ERR("failed to open \"%s\": %s", cfg_name, strerror(errno));
+        svec_terminate(&cfg);
         return errno;
     }
 
index 42345f8..e159244 100644 (file)
--- a/lib/cfg.h
+++ b/lib/cfg.h
@@ -26,6 +26,7 @@
 struct svec;
 struct ofpbuf;
 
+void cfg_init(void);
 int cfg_set_file(const char *file_name);
 int cfg_read(void);
 int cfg_lock(uint8_t *cookie, int timeout);