Cosmetic improvements in new config_info code.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 21 Feb 2016 16:38:24 +0000 (11:38 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 21 Feb 2016 16:38:24 +0000 (11:38 -0500)
Coverity griped about use of unchecked strcpy() into a local variable.
There's unlikely to be any actual bug there, since no caller would be
passing a path longer than MAXPGPATH, but nonetheless use of strlcpy()
seems preferable.

While at it, get rid of unmaintainable separation between list of
field names and list of field values in favor of initializing them
in parallel.  And we might as well declare get_configdata()'s path
argument as const char *, even though no current caller needs that.

src/common/config_info.c
src/include/common/config_info.h

index 9053a8cbe48ebe01d71c0382bbd797701896af3e..8976b3d288c3b8da95fc057b9d47816ac42e10fd 100644 (file)
 #include "postgres_fe.h"
 #endif
 
-#include "miscadmin.h"
 #include "common/config_info.h"
+#include "miscadmin.h"
 
-static size_t configdata_names_len(void);
-
-static const char *const configdata_names[] =
-{
-       "BINDIR",
-       "DOCDIR",
-       "HTMLDIR",
-       "INCLUDEDIR",
-       "PKGINCLUDEDIR",
-       "INCLUDEDIR-SERVER",
-       "LIBDIR",
-       "PKGLIBDIR",
-       "LOCALEDIR",
-       "MANDIR",
-       "SHAREDIR",
-       "SYSCONFDIR",
-       "PGXS",
-       "CONFIGURE",
-       "CC",
-       "CPPFLAGS",
-       "CFLAGS",
-       "CFLAGS_SL",
-       "LDFLAGS",
-       "LDFLAGS_EX",
-       "LDFLAGS_SL",
-       "LIBS",
-       "VERSION",
-       NULL
-};
-
-static size_t
-configdata_names_len(void)
-{
-       size_t  i = 0;
-
-       while (configdata_names[i])
-               i++;
-
-       return i;
-}
 
 /*
- * get_configdata(char *my_exec_path, size_t *configdata_len)
+ * get_configdata(const char *my_exec_path, size_t *configdata_len)
  *
  * Get configure-time constants. The caller is responsible
  * for pfreeing the result.
  */
 ConfigData *
-get_configdata(char *my_exec_path, size_t *configdata_len)
+get_configdata(const char *my_exec_path, size_t *configdata_len)
 {
        ConfigData         *configdata;
        char                    path[MAXPGPATH];
        char               *lastsep;
-       int                             i;
+       int                             i = 0;
 
-       *configdata_len = configdata_names_len();
-       configdata = palloc(*configdata_len * sizeof(ConfigData));
+       /* Adjust this to match the number of items filled below */
+       *configdata_len = 23;
+       configdata = (ConfigData *) palloc(*configdata_len * sizeof(ConfigData));
 
-       /*
-        * initialize configdata names
-        *
-        * These better be in sync with the settings manually
-        * defined below.
-        */
-       for (i = 0; i < *configdata_len; i++)
-               configdata[i].name = pstrdup(configdata_names[i]);
-
-       strcpy(path, my_exec_path);
+       configdata[i].name = pstrdup("BINDIR");
+       strlcpy(path, my_exec_path, sizeof(path));
        lastsep = strrchr(path, '/');
        if (lastsep)
                *lastsep = '\0';
        cleanup_path(path);
-       configdata[0].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("DOCDIR");
        get_doc_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[1].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("HTMLDIR");
        get_html_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[2].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("INCLUDEDIR");
        get_include_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[3].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("PKGINCLUDEDIR");
        get_pkginclude_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[4].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("INCLUDEDIR-SERVER");
        get_includeserver_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[5].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("LIBDIR");
        get_lib_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[6].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("PKGLIBDIR");
        get_pkglib_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[7].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("LOCALEDIR");
        get_locale_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[8].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("MANDIR");
        get_man_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[9].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("SHAREDIR");
        get_share_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[10].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("SYSCONFDIR");
        get_etc_path(my_exec_path, path);
        cleanup_path(path);
-       configdata[11].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("PGXS");
        get_pkglib_path(my_exec_path, path);
        strlcat(path, "/pgxs/src/makefiles/pgxs.mk", sizeof(path));
        cleanup_path(path);
-       configdata[12].setting = pstrdup(path);
+       configdata[i].setting = pstrdup(path);
+       i++;
 
+       configdata[i].name = pstrdup("CONFIGURE");
 #ifdef VAL_CONFIGURE
-       configdata[13].setting = pstrdup(VAL_CONFIGURE);
+       configdata[i].setting = pstrdup(VAL_CONFIGURE);
 #else
-       configdata[13].setting = pstrdup(_("not recorded"));
+       configdata[i].setting = pstrdup(_("not recorded"));
 #endif
+       i++;
 
+       configdata[i].name = pstrdup("CC");
 #ifdef VAL_CC
-       configdata[14].setting = pstrdup(VAL_CC);
+       configdata[i].setting = pstrdup(VAL_CC);
 #else
-       configdata[14].setting = pstrdup(_("not recorded"));
+       configdata[i].setting = pstrdup(_("not recorded"));
 #endif
+       i++;
 
+       configdata[i].name = pstrdup("CPPFLAGS");
 #ifdef VAL_CPPFLAGS
-       configdata[15].setting = pstrdup(VAL_CPPFLAGS);
+       configdata[i].setting = pstrdup(VAL_CPPFLAGS);
 #else
-       configdata[15].setting = pstrdup(_("not recorded"));
+       configdata[i].setting = pstrdup(_("not recorded"));
 #endif
+       i++;
 
+       configdata[i].name = pstrdup("CFLAGS");
 #ifdef VAL_CFLAGS
-       configdata[16].setting = pstrdup(VAL_CFLAGS);
+       configdata[i].setting = pstrdup(VAL_CFLAGS);
 #else
-       configdata[16].setting = pstrdup(_("not recorded"));
+       configdata[i].setting = pstrdup(_("not recorded"));
 #endif
+       i++;
 
+       configdata[i].name = pstrdup("CFLAGS_SL");
 #ifdef VAL_CFLAGS_SL
-       configdata[17].setting = pstrdup(VAL_CFLAGS_SL);
+       configdata[i].setting = pstrdup(VAL_CFLAGS_SL);
 #else
-       configdata[17].setting = pstrdup(_("not recorded"));
+       configdata[i].setting = pstrdup(_("not recorded"));
 #endif
+       i++;
 
+       configdata[i].name = pstrdup("LDFLAGS");
 #ifdef VAL_LDFLAGS
-       configdata[18].setting = pstrdup(VAL_LDFLAGS);
+       configdata[i].setting = pstrdup(VAL_LDFLAGS);
 #else
-       configdata[18].setting = pstrdup(_("not recorded"));
+       configdata[i].setting = pstrdup(_("not recorded"));
 #endif
+       i++;
 
+       configdata[i].name = pstrdup("LDFLAGS_EX");
 #ifdef VAL_LDFLAGS_EX
-       configdata[19].setting = pstrdup(VAL_LDFLAGS_EX);
+       configdata[i].setting = pstrdup(VAL_LDFLAGS_EX);
 #else
-       configdata[19].setting = pstrdup(_("not recorded"));
+       configdata[i].setting = pstrdup(_("not recorded"));
 #endif
+       i++;
 
+       configdata[i].name = pstrdup("LDFLAGS_SL");
 #ifdef VAL_LDFLAGS_SL
-       configdata[20].setting = pstrdup(VAL_LDFLAGS_SL);
+       configdata[i].setting = pstrdup(VAL_LDFLAGS_SL);
 #else
-       configdata[20].setting = pstrdup(_("not recorded"));
+       configdata[i].setting = pstrdup(_("not recorded"));
 #endif
+       i++;
 
+       configdata[i].name = pstrdup("LIBS");
 #ifdef VAL_LIBS
-       configdata[21].setting = pstrdup(VAL_LIBS);
+       configdata[i].setting = pstrdup(VAL_LIBS);
 #else
-       configdata[21].setting = pstrdup(_("not recorded"));
+       configdata[i].setting = pstrdup(_("not recorded"));
 #endif
+       i++;
+
+       configdata[i].name = pstrdup("VERSION");
+       configdata[i].setting = pstrdup("PostgreSQL " PG_VERSION);
+       i++;
 
-       configdata[22].setting = pstrdup("PostgreSQL " PG_VERSION);
+       Assert(i == *configdata_len);
 
        return configdata;
 }
index 649ef5cf4bec7fb9781a0f1ea75023c226677f8b..c9e6e1cc8f8e7039423c43c42062701623f4e7ad 100644 (file)
@@ -15,7 +15,7 @@ typedef struct ConfigData
        char       *setting;
 } ConfigData;
 
-extern ConfigData *get_configdata(char *my_exec_path,
+extern ConfigData *get_configdata(const char *my_exec_path,
                                                                  size_t *configdata_len);
 
 #endif   /* COMMON_CONFIG_INFO_H */