Subject: [postgresql-interfaces/psqlodbc] issue #99: possible use of uninitialized memory in GetExeProgramName()

From: "lvoege (@lvoege)" <[email-masked]>
Date: Wed, 26 Mar 2025 00:08:35 +0000

while pointing valgrind at something unrelated it spotted the use of uninitialized memory in `po_basename()` in `mylog.c`. this is because `GetExeProgramName()` calls `readlink()`, `readlink()` doesn't add a terminating null and `GetExeProgramName()` doesn't add one itself, so the `strrchr()` in `po_basename()` can then start from garbage.

this fixes it:

~~~
diff --git a/mylog.c b/mylog.c
index 9377ad2..66e24a9 100644
--- a/mylog.c
+++ b/mylog.c
@@ -133,8 +133,10 @@ const char *GetExeProgramName()
 
                for (i = 0; i < sizeof(flist) / sizeof(flist[0]); i++)
                {
-                       if (readlink(flist[i], path_name, sizeof(path_name)) > 0)
+                       ssize_t len = readlink(flist[i], path_name, sizeof(path_name));
+                       if (len > 0)
                        {
+                               path_name[len] = 0;
                                /* fprintf(stderr, "i=%d pathname=%s\n", i, path_name); */
                                STRCPY_FIXED(exename, po_basename(path_name));
                                break;

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Thu, 27 Mar 2025 09:38:11 +0000

closed with https://github.com/postgresql-interfaces/psqlodbc/pull/100