Changeset: f67d1b8fc6c4 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/f67d1b8fc6c4
Modified Files:
        clients/mapiclient/dump.c
        clients/mapiclient/mclient.c
        clients/mapilib/connect.c
        clients/mapilib/mapi.c
        clients/mapilib/mapi_intern.h
        clients/mapilib/msettings.c
        clients/odbc/driver/SQLConnect.c
        clients/odbc/driver/SQLDriverConnect.c
        clients/odbc/driver/SQLPrimaryKeys.c
        common/utils/mutils.c
Branch: Aug2024
Log Message:

Coverity inspired fixes.


diffs (truncated from 382 to 300 lines):

diff --git a/clients/mapiclient/dump.c b/clients/mapiclient/dump.c
--- a/clients/mapiclient/dump.c
+++ b/clients/mapiclient/dump.c
@@ -1286,6 +1286,8 @@ describe_table(Mapi mid, const char *sch
 
        s = sescape(schema);
        t = sescape(tname);
+       if (s == NULL || t == NULL)
+               goto bailout;
        maxquerylen = 5120 + strlen(t) + strlen(s);
        query = malloc(maxquerylen);
        if (query == NULL)
@@ -2249,7 +2251,11 @@ dump_table(Mapi mid, const char *schema,
                                goto doreturn;
                        }
                        for (int64_t i = 0; i < rows; i++) {
-                               mapi_fetch_row(hdl);
+                               if (mapi_fetch_row(hdl) == 0) {
+                                       mapi_close_handle(hdl);
+                                       fprintf(stderr, "unexepcted error\n");
+                                       goto doreturn;
+                               }
                                tables[i].schema = strdup(mapi_fetch_field(hdl, 
0));
                                tables[i].table = strdup(mapi_fetch_field(hdl, 
1));
                                if (tables[i].schema == NULL || tables[i].table 
== NULL) {
@@ -3365,7 +3371,6 @@ dump_database(Mapi mid, stream *sqlf, co
                        mnstr_printf(sqlf, " %sCYCLE;\n", strcmp(cycle, "true") 
== 0 ? "" : "NO ");
                        if (mnstr_errnr(sqlf) != MNSTR_NO__ERROR) {
                                mapi_close_handle(hdl);
-                               hdl = NULL;
                                goto bailout2;
                        }
                }
diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -3223,7 +3223,7 @@ putfile(void *data, const char *filename
                close_stream(priv->f);
                priv->f = NULL;
                if (fname) {
-                       MT_remove(fname);
+                       (void) MT_remove(fname);
                        free(fname);
                }
                if (filename == NULL)
diff --git a/clients/mapilib/connect.c b/clients/mapilib/connect.c
--- a/clients/mapilib/connect.c
+++ b/clients/mapilib/connect.c
@@ -430,7 +430,7 @@ send_all_clientinfo(Mapi mid)
 
        if (hostname[0])
                reallocprintf(&buf, &pos, &cap, "ClientHostname=%s\n", 
hostname);
-       if (application_name[0])
+       if (application_name && application_name[0])
                reallocprintf(&buf, &pos, &cap, "ApplicationName=%s\n", 
application_name);
        reallocprintf(&buf, &pos, &cap, "ClientLibrary=");
        if (mid->clientprefix)
@@ -466,7 +466,7 @@ mapi_handshake(Mapi mid)
 
        /* consume server challenge */
        len = mnstr_read_block(mid->from, buf, 1, sizeof(buf));
-       check_stream(mid, mid->from, "Connection terminated while starting 
handshake", (mid->blk.eos = true, mid->error));
+       check_stream(mid, mid->from, len, "Connection terminated while starting 
handshake", (mid->blk.eos = true, mid->error));
 
        mapi_log_data(mid, "RECV HANDSHAKE", buf, len);
 
@@ -603,7 +603,6 @@ mapi_handshake(Mapi mid)
                        pwdhash = mcrypt_SHA1Sum(password,
                                                        strlen(password));
                } else {
-                       (void)pwdhash;
                        snprintf(buf, sizeof(buf), "server requires unknown 
hash '%.100s'",
                                        serverhash);
                        close_connection(mid);
@@ -619,6 +618,7 @@ mapi_handshake(Mapi mid)
 
                char *replacement_password = malloc(1 + strlen(pwdhash) + 1);
                if (replacement_password == NULL) {
+                       free(pwdhash);
                        close_connection(mid);
                        return mapi_setError(mid, "malloc failed", __func__, 
MERROR);
                }
@@ -722,10 +722,10 @@ mapi_handshake(Mapi mid)
 
        len = strlen(buf);
        mapi_log_data(mid, "HANDSHAKE SEND", buf, len);
-       mnstr_write(mid->to, buf, 1, len);
-       check_stream(mid, mid->to, "Could not send initial byte sequence", 
mid->error);
-       mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
-       check_stream(mid, mid->to, "Could not send initial byte sequence", 
mid->error);
+       len = mnstr_write(mid->to, buf, 1, len);
+       check_stream(mid, mid->to, len, "Could not send initial byte sequence", 
mid->error);
+       len = mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
+       check_stream(mid, mid->to, len, "Could not send initial byte sequence", 
mid->error);
 
        // Clear the redirects before we receive new ones
        for (char **r = mid->redirects; *r != NULL; r++) {
diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -1682,8 +1682,8 @@ finish_handle(MapiHdl hdl)
                        assert(mid->active == NULL || mid->active == hdl);
                        hdl->needmore = false;
                        mid->active = hdl;
-                       mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
-                       check_stream(mid, mid->to, "write error on stream", 
mid->error);
+                       int f = mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
+                       check_stream(mid, mid->to, f, "write error on stream", 
mid->error);
                        read_into_cache(hdl, 0);
                }
                for (i = 0; i < hdl->npending_close; i++) {
@@ -1712,8 +1712,8 @@ finish_handle(MapiHdl hdl)
                        assert(mid->active == NULL || mid->active == hdl);
                        hdl->needmore = false;
                        mid->active = hdl;
-                       mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
-                       check_stream(mid, mid->to, "write error on stream", 
mid->error);
+                       int f = mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
+                       check_stream(mid, mid->to, f, "write error on stream", 
mid->error);
                        read_into_cache(hdl, 0);
                }
        }
@@ -1795,12 +1795,12 @@ mapi_new(msettings *settings)
 
        /* then fill in some details */
        *mid = MapiStructDefaults;
-       mid->settings = settings;
        mid->index =  (uint32_t) ATOMIC_ADD(&index, 1); /* for distinctions in 
log records */
        if ((mid->blk.buf = malloc(mid->blk.lim + 1)) == NULL) {
                mapi_destroy(mid);
                return NULL;
        }
+       mid->settings = settings;
        mid->blk.buf[0] = 0;
        mid->blk.buf[mid->blk.lim] = 0;
 
@@ -2725,7 +2725,7 @@ read_line(Mapi mid)
                        } else
                                break;
                }
-               check_stream(mid, mid->from, "Connection terminated during read 
line", (mid->blk.eos = true, (char *) 0));
+               check_stream(mid, mid->from, len, "Connection terminated during 
read line", (mid->blk.eos = true, (char *) 0));
                mapi_log_data(mid, "RECV", mid->blk.buf + mid->blk.end, len);
                mid->blk.buf[mid->blk.end + len] = 0;
                if (mid->trace) {
@@ -3429,8 +3429,8 @@ read_into_cache(MapiHdl hdl, int lookahe
        assert(mid->active == hdl);
        if (hdl->needmore) {
                hdl->needmore = false;
-               mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
-               check_stream(mid, mid->to, "write error on stream", mid->error);
+               int f = mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
+               check_stream(mid, mid->to, f, "write error on stream", 
mid->error);
        }
        if ((result = hdl->active) == NULL)
                result = hdl->result;   /* may also be NULL */
@@ -3565,21 +3565,21 @@ mapi_execute_internal(MapiHdl hdl)
 
        if (is_sql) {
                /* indicate to server this is a SQL command */
-               mnstr_write(mid->to, "s", 1, 1);
-               check_stream(mid, mid->to, "write error on stream", mid->error);
+               ssize_t w = mnstr_write(mid->to, "s", 1, 1);
+               check_stream(mid, mid->to, w, "write error on stream", 
mid->error);
        }
-       mnstr_write(mid->to, cmd, 1, size);
-       check_stream(mid, mid->to, "write error on stream", mid->error);
+       ssize_t w = mnstr_write(mid->to, cmd, 1, size);
+       check_stream(mid, mid->to, w, "write error on stream", mid->error);
        /* all SQL statements should end with a semicolon */
        /* for the other languages it is assumed that the statements are 
correct */
        if (is_sql) {
-               mnstr_write(mid->to, "\n;", 2, 1);
-               check_stream(mid, mid->to, "write error on stream", mid->error);
+               w = mnstr_write(mid->to, "\n;", 2, 1);
+               check_stream(mid, mid->to, w, "write error on stream", 
mid->error);
        }
-       mnstr_write(mid->to, "\n", 1, 1);
-       check_stream(mid, mid->to, "write error on stream", mid->error);
-       mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
-       check_stream(mid, mid->to, "write error on stream", mid->error);
+       w = mnstr_write(mid->to, "\n", 1, 1);
+       check_stream(mid, mid->to, w, "write error on stream", mid->error);
+       w = mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
+       check_stream(mid, mid->to, w, "write error on stream", mid->error);
        mid->active = hdl;
 
        return MOK;
@@ -3703,12 +3703,12 @@ mapi_query_part(MapiHdl hdl, const char 
                printf("mapi_query_part:%zu:%.*s\n", size, (int) size, query);
        }
        hdl->needmore = false;
-       mnstr_write(mid->to, query, 1, size);
+       size = mnstr_write(mid->to, query, 1, size);
        if (mid->tracelog) {
                mnstr_write(mid->tracelog, query, 1, size);
                mnstr_flush(mid->tracelog, MNSTR_FLUSH_DATA);
        }
-       check_stream(mid, mid->to, "write error on stream", mid->error);
+       check_stream(mid, mid->to, size, "write error on stream", mid->error);
        return mid->error;
 }
 
@@ -3723,8 +3723,8 @@ mapi_query_done(MapiHdl hdl)
        assert(mid->active == NULL || mid->active == hdl);
        mid->active = hdl;
        hdl->needmore = false;
-       mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
-       check_stream(mid, mid->to, "write error on stream", mid->error);
+       int f = mnstr_flush(mid->to, MNSTR_FLUSH_DATA);
+       check_stream(mid, mid->to, f, "write error on stream", mid->error);
        ret = mid->error;
        if (ret == MOK)
                ret = read_into_cache(hdl, 1);
@@ -3911,11 +3911,12 @@ mapi_fetch_line(MapiHdl hdl)
                mapi_log_record(hdl->mid, "W", "X" "export %d %" PRId64 "\n",
                                     result->tableid,
                                     result->cache.first + 
result->cache.tuplecount);
-               if (mnstr_printf(hdl->mid->to, "X" "export %d %" PRId64 "\n",
+               int e;
+               if ((e = mnstr_printf(hdl->mid->to, "X" "export %d %" PRId64 
"\n",
                                 result->tableid,
-                                result->cache.first + 
result->cache.tuplecount) < 0 ||
-                   mnstr_flush(hdl->mid->to, MNSTR_FLUSH_DATA))
-                       check_stream(hdl->mid, hdl->mid->to, "sending export 
command", NULL);
+                                     result->cache.first + 
result->cache.tuplecount)) < 0 ||
+                   (e = mnstr_flush(hdl->mid->to, MNSTR_FLUSH_DATA)) < 0)
+                       check_stream(hdl->mid, hdl->mid->to, e, "sending export 
command", NULL);
                reply = mapi_fetch_line_internal(hdl);
        }
        return reply;
@@ -4433,10 +4434,11 @@ mapi_fetch_all_rows(MapiHdl hdl)
                        hdl->active = result;
                        mapi_log_record(mid, "SEND", "X" "export %d %" PRId64 
"\n",
                                             result->tableid, 
result->cache.first + result->cache.tuplecount);
-                       if (mnstr_printf(mid->to, "X" "export %d %" PRId64 "\n",
-                                        result->tableid, result->cache.first + 
result->cache.tuplecount) < 0 ||
-                           mnstr_flush(mid->to, MNSTR_FLUSH_DATA))
-                               check_stream(mid, mid->to, "sending export 
command", 0);
+                       int e;
+                       if ((e = mnstr_printf(mid->to, "X" "export %d %" PRId64 
"\n",
+                                             result->tableid, 
result->cache.first + result->cache.tuplecount)) < 0 ||
+                           (e = mnstr_flush(mid->to, MNSTR_FLUSH_DATA)) < 0)
+                               check_stream(mid, mid->to, e, "sending export 
command", 0);
                }
                if (mid->active)
                        read_into_cache(mid->active, 0);
diff --git a/clients/mapilib/mapi_intern.h b/clients/mapilib/mapi_intern.h
--- a/clients/mapilib/mapi_intern.h
+++ b/clients/mapilib/mapi_intern.h
@@ -283,9 +283,9 @@ void mapi_impl_log_record(Mapi mid, cons
 #define mapi_log_data(mid, mark, start, len)  do { if ((mid)->tracelog) 
mapi_impl_log_data(mid, __func__, __LINE__, mark, start, len); } while (0)
 #define mapi_log_record(mid, mark, ...)  do { if ((mid)->tracelog) 
mapi_impl_log_record(mid, __func__, __LINE__, mark, __VA_ARGS__); } while (0)
 
-#define check_stream(mid, s, msg, e)                                   \
+#define check_stream(mid, s, l, msg, e)                                        
\
        do {                                                            \
-               if ((s) == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR) { \
+               if ((s) == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR || 
(ssize_t) (l) < 0) { \
                        if (mnstr_peek_error(s))                        \
                                mapi_printError((mid), __func__, MTIMEOUT, "%s: 
%s", (msg), mnstr_peek_error(s)); \
                        else                                            \
diff --git a/clients/mapilib/msettings.c b/clients/mapilib/msettings.c
--- a/clients/mapilib/msettings.c
+++ b/clients/mapilib/msettings.c
@@ -418,12 +418,16 @@ void msettings_set_localizer(msettings *
 const char*
 msetting_string(const msettings *mp, mparm parm)
 {
-       if (mparm_classify(parm) != MPCLASS_STRING)
+       if (mparm_classify(parm) != MPCLASS_STRING) {
                FATAL();
+               return "";
+       }
        int i = parm - MP__STRING_START;
        struct string const *p = &mp->dummy_start_string + 1 + i;
-       if (p >=  &mp->dummy_end_string)
+       if (p >=  &mp->dummy_end_string) {
                FATAL();
+               return "";
+       }
        char *s = p->str;
 
        if (s == NULL) {
diff --git a/clients/odbc/driver/SQLConnect.c b/clients/odbc/driver/SQLConnect.c
--- a/clients/odbc/driver/SQLConnect.c
+++ b/clients/odbc/driver/SQLConnect.c
@@ -311,7 +311,6 @@ end:
 
 
 SQLRETURN
-
 MNDBConnect(ODBCDbc *dbc,
            const SQLCHAR *ServerName,
            SQLSMALLINT NameLength1,
@@ -324,10 +323,10 @@ MNDBConnect(ODBCDbc *dbc,
        // If unset, 'goto failure' will assume an allocation error.
        const char *error_state = NULL;
        const char *error_explanation = NULL;
+       SQLRETURN ret = SQL_ERROR;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to