Check return value of stdup() for out-of-memory.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Tue, 12 Nov 2013 21:57:29 +0000 (23:57 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Tue, 12 Nov 2013 21:57:29 +0000 (23:57 +0200)
There are a lot of little bugs like this throughout the code, but it's
start...

Flagged by Fortify static analysis tool.

connection.c

index 62393029e2b45f61c2d35b851189c5f7b3989543..28e2636745b59a9212d490b49fd2c1749122faec 100644 (file)
@@ -3734,57 +3734,64 @@ CC_send_settings(ConnectionClass *self)
    if (NAME_IS_VALID(ci->drivers.conn_settings))
    {
        cs = strdup(GET_NAME(ci->drivers.conn_settings));
+       if (cs)
+       {
 #ifdef HAVE_STRTOK_R
-       ptr = strtok_r(cs, semi_colon, &last);
+           ptr = strtok_r(cs, semi_colon, &last);
 #else
-       ptr = strtok(cs, semi_colon);
+           ptr = strtok(cs, semi_colon);
 #endif /* HAVE_STRTOK_R */
-       while (ptr)
-       {
-           result = PGAPI_ExecDirect(hstmt, ptr, SQL_NTS, 0);
-           if (!SQL_SUCCEEDED(result))
-               status = FALSE;
+           while (ptr)
+           {
+               result = PGAPI_ExecDirect(hstmt, ptr, SQL_NTS, 0);
+               if (!SQL_SUCCEEDED(result))
+                   status = FALSE;
 
-           mylog("%s: result %d, status %d from '%s'\n", func, result, status, ptr);
+               mylog("%s: result %d, status %d from '%s'\n", func, result, status, ptr);
 
 #ifdef HAVE_STRTOK_R
-           ptr = strtok_r(NULL, semi_colon, &last);
+               ptr = strtok_r(NULL, semi_colon, &last);
 #else
-           ptr = strtok(NULL, semi_colon);
+               ptr = strtok(NULL, semi_colon);
 #endif /* HAVE_STRTOK_R */
+           }
+           free(cs);
        }
-
-       free(cs);
+       else
+           status = FALSE;
    }
 
    /* Per Datasource settings */
    if (NAME_IS_VALID(ci->conn_settings))
    {
        cs = strdup(GET_NAME(ci->conn_settings));
+       if (cs)
+       {
 #ifdef HAVE_STRTOK_R
-       ptr = strtok_r(cs, semi_colon, &last);
+           ptr = strtok_r(cs, semi_colon, &last);
 #else
-       ptr = strtok(cs, semi_colon);
+           ptr = strtok(cs, semi_colon);
 #endif /* HAVE_STRTOK_R */
-       while (ptr)
-       {
-           result = PGAPI_ExecDirect(hstmt, ptr, SQL_NTS, 0);
-           if (!SQL_SUCCEEDED(result))
-               status = FALSE;
+           while (ptr)
+           {
+               result = PGAPI_ExecDirect(hstmt, ptr, SQL_NTS, 0);
+               if (!SQL_SUCCEEDED(result))
+                   status = FALSE;
 
-           mylog("%s: result %d, status %d from '%s'\n", func, result, status, ptr);
+               mylog("%s: result %d, status %d from '%s'\n", func, result, status, ptr);
 
 #ifdef HAVE_STRTOK_R
-           ptr = strtok_r(NULL, semi_colon, &last);
+               ptr = strtok_r(NULL, semi_colon, &last);
 #else
-           ptr = strtok(NULL, semi_colon);
+               ptr = strtok(NULL, semi_colon);
 #endif /* HAVE_STRTOK_R */
+           }
+           free(cs);
        }
-
-       free(cs);
+       else
+           status = FALSE;
    }
 
-
    PGAPI_FreeStmt(hstmt, SQL_DROP);
 
    return status;