X-Original-To: pgsql-odbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 378885287F for ; Fri, 22 Jul 2005 12:19:03 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 09549-04 for ; Fri, 22 Jul 2005 15:19:01 +0000 (GMT) Received: from mxfep02.bredband.com (mxfep02.bredband.com [195.54.107.73]) by svr1.postgresql.org (Postfix) with ESMTP id 83E885286A for ; Fri, 22 Jul 2005 12:18:59 -0300 (ADT) Received: from junk.nocrew.org ([213.115.110.229] [213.115.110.229]) by mxfep02.bredband.com with ESMTP id <20050722151858.BWUF6295.mxfep02.bredband.com@junk.nocrew.org> for ; Fri, 22 Jul 2005 17:18:58 +0200 Received: from tomas by junk.nocrew.org with local (Exim 4.50) id 1DvzIg-0007pi-CF for pgsql-odbc@postgresql.org; Fri, 22 Jul 2005 17:18:58 +0200 To: pgsql-odbc@postgresql.org Subject: Static analysis run From: tomas@nocrew.org (=?iso-8859-1?q?Tomas_Sk=E4re?=) Date: Fri, 22 Jul 2005 17:18:57 +0200 Message-ID: <80ackehnry.fsf@junk.nocrew.org> User-Agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200507/115 X-Sequence-Number: 5581 --=-=-= Hi, We use psqlodbc in one of our projects at my company and we recently bought a static source code analysis program (Klocwork) to run on our own software. Since we use psqlodbc, I also ran it on that and it found a few things. This was run on the CVS head branch from this today (fri 22 july). Attached is a diff with some fixes that I managed myself. Then I also have some places that were not as easy for me to fix, but maybe some of you that know the code better can do it. Here is a list with things, some might not be correct, since this tool sometimes thinks there's an error where there aren't. Row numbers might not be exact because of some local changes, but they should be almost right. convert.c in ResolveOneParam:2651 allocbuf is allocated, further down (2785), CVT_APPEND_CHAR and CVT_APPEND_STR are used a lot, and these macros may return from the function, giving a memory leak for allocbuf. environ.c in PGAPI_FreeEnv:89 env is freed even if EN_Destructor returns false above, which means it's accessing freed memory. info.c in PGAPI_Statistics:2623 If SC_MALLOC_return_with_error returns, column_names allocated above is lost. odbcapi30w.c in SQLGetDescFieldW:128 blen is not initialised before sent to utf8_to_ucs2(). The same thing exists in SQLColAttributeW:287 and SQLGetDiagFieldW:345. results.c in SC_pos_reload_needed:2189 rows_per_fetch is uninitialised, if create_from_scratch is false. These are the things that I saw just looking through the report pretty quickly. There are a lot of reports about dereferenced NULL pointers, but I haven't looked more closely at any of those. Most of them comes from not checking what malloc and family returns. It also complains a lot about strcpy and sprintf not checking boundaries. Those might not at all be errors, but then again they could cause buffer overflows. If you are interested, I can send the full report (200KB html, 295 entries including the ones in the patch and the above things), and if someone has time, that person can go through the list. Greetings, Tomas --=-=-= Content-Disposition: attachment; filename=kw.diff Content-Description: bugfixes Index: connection.c =================================================================== RCS file: /usr/local/cvsroot/psqlodbc/psqlodbc/connection.c,v retrieving revision 1.97 diff -u -r1.97 connection.c --- connection.c 20 Jul 2005 14:50:21 -0000 1.97 +++ connection.c 22 Jul 2005 14:46:32 -0000 @@ -2360,13 +2360,20 @@ CC_conninfo_init(&(rv->connInfo)); rv->stmts = (StatementClass **) malloc(sizeof(StatementClass *) * STMT_INCREMENT); if (!rv->stmts) + { + free(rv); return NULL; + } memset(rv->stmts, 0, sizeof(StatementClass *) * STMT_INCREMENT); rv->num_stmts = STMT_INCREMENT; rv->descs = (DescriptorClass **) malloc(sizeof(DescriptorClass *) * STMT_INCREMENT); if (!rv->descs) + { + free(rv->stmts); + free(rv); return NULL; + } memset(rv->descs, 0, sizeof(DescriptorClass *) * STMT_INCREMENT); rv->num_descs = STMT_INCREMENT; @@ -2857,6 +2864,7 @@ if((!res) || (res->status == PGRES_EMPTY_QUERY) ) { QR_Destructor(res); + res = NULL; goto cleanup; } else @@ -3141,6 +3149,7 @@ { CC_set_error(self, CONNECTION_COULD_NOT_RECEIVE, "Could not allocate memory for result set"); QR_Destructor(qres); + return NULL; } qres->status = PQresultStatus(pgres); if( (PQresultStatus(pgres) == PGRES_COMMAND_OK) ) Index: convert.c =================================================================== RCS file: /usr/local/cvsroot/psqlodbc/psqlodbc/convert.c,v retrieving revision 1.126 diff -u -r1.126 convert.c --- convert.c 15 Jul 2005 09:08:50 -0000 1.126 +++ convert.c 22 Jul 2005 14:46:33 -0000 @@ -1578,7 +1578,7 @@ while (qp->statement[qp->opos] != '\0' && qp->statement[qp->opos] != ch) \ { \ buf[c++] = qp->statement[qp->opos++]; \ - if (c >= maxsize) \ + if (c >= maxsize - 1) \ break; \ } \ if (qp->statement[qp->opos] == '\0') \ Index: info.c =================================================================== RCS file: /usr/local/cvsroot/psqlodbc/psqlodbc/info.c,v retrieving revision 1.121 diff -u -r1.121 info.c --- info.c 13 Jul 2005 14:23:35 -0000 1.121 +++ info.c 22 Jul 2005 14:46:33 -0000 @@ -1304,7 +1304,7 @@ #else prefix[i] = strtok(prefixes, ";"); #endif /* HAVE_STRTOK_R */ - while (prefix[i] && i < sizeof(prefix)) + while (i < 31 && prefix[i]) #ifdef HAVE_STRTOK_R prefix[++i] = strtok_r(NULL, ";", &last); #else @@ -1328,7 +1328,7 @@ #else table_type[i] = strtok(table_types, ","); #endif /* HAVE_STRTOK_R */ - while (table_type[i] && i < 32) + while (i < 31 && table_type[i]) #ifdef HAVE_STRTOK_R table_type[++i] = strtok_r(NULL, ",", &last); #else @@ -3815,6 +3815,7 @@ } if (pkey_alloced) free(pkey_text); + pkey_alloced = FALSE; /* Get to next primary key */ for (k = 0; k < 2; k++) pkey_ptr += strlen(pkey_ptr) + 1; --=-=-=--