Changeset: 9d2f1cc41772 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=9d2f1cc41772
Modified Files:
        clients/mapilib/mapi.c
        common/stream/stream.c
Branch: default
Log Message:

A little code cleanup.


diffs (214 lines):

diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -1810,14 +1810,13 @@ mapi_new(void)
                .redirmax = 10,
                .blk.eos = false,
                .blk.lim = BLOCK,
-               .blk.buf = malloc(BLOCK + 1),
        };
-       if (mid->blk.buf == NULL) {
+       if ((mid->blk.buf = malloc(mid->blk.lim + 1)) == NULL) {
                mapi_destroy(mid);
                return NULL;
        }
-       mid->blk.buf[BLOCK] = 0;
        mid->blk.buf[0] = 0;
+       mid->blk.buf[mid->blk.lim] = 0;
 
        return mid;
 }
@@ -2161,9 +2160,6 @@ mapi_reconnect(Mapi mid)
 
                char *host;
                int port;
-#ifdef HAVE_SYS_UN_H
-               char buf[1024];
-#endif
 
                host = mid->hostname;
                port = mid->port;
@@ -2300,7 +2296,6 @@ mapi_reconnect(Mapi mid)
        if (mid->hostname && mid->hostname[0] == '/') {
                struct msghdr msg;
                struct iovec vec;
-               char buf[1];
                struct sockaddr_un userver;
                struct sockaddr *serv = (struct sockaddr *) &userver;
 
@@ -2348,7 +2343,7 @@ mapi_reconnect(Mapi mid)
                /* send first byte, nothing special to happen */
                msg.msg_name = NULL;
                msg.msg_namelen = 0;
-               *buf = '0';     /* normal */
+               buf[0] = '0';   /* normal */
                vec.iov_base = buf;
                vec.iov_len = 1;
                msg.msg_iov = &vec;
@@ -2494,11 +2489,11 @@ mapi_reconnect(Mapi mid)
   try_again_after_redirect:
 
        /* consume server challenge */
-       len = mnstr_read_block(mid->from, buf, 1, BLOCK);
+       len = mnstr_read_block(mid->from, buf, 1, sizeof(buf));
 
        check_stream(mid, mid->from, "Connection terminated while starting", 
"mapi_reconnect", (mid->blk.eos = true, mid->error));
 
-       assert(len < BLOCK);
+       assert(len < sizeof(buf));
        buf[len] = 0;
 
        if (len == 0) {
@@ -2638,7 +2633,7 @@ mapi_reconnect(Mapi mid)
                        } else
 #endif
                        {
-                               snprintf(buf, BLOCK, "server requires unknown 
hash '%.100s'",
+                               snprintf(buf, sizeof(buf), "server requires 
unknown hash '%.100s'",
                                                serverhash);
                                close_connection(mid);
                                return mapi_setError(mid, buf, 
"mapi_reconnect", MERROR);
@@ -2673,7 +2668,7 @@ mapi_reconnect(Mapi mid)
                }
                if (hash == NULL) {
                        /* the server doesn't support what we can */
-                       snprintf(buf, BLOCK, "unsupported hash algorithms: 
%.100s", hashes);
+                       snprintf(buf, sizeof(buf), "unsupported hash 
algorithms: %.100s", hashes);
                        close_connection(mid);
                        return mapi_setError(mid, buf, "mapi_reconnect", 
MERROR);
                }
@@ -2682,14 +2677,14 @@ mapi_reconnect(Mapi mid)
 
                /* note: if we make the database field an empty string, it
                 * means we want the default.  However, it *should* be there. */
-               if (snprintf(buf, BLOCK, "%s:%s:%s:%s:%s:FILETRANS:\n",
+               if (snprintf(buf, sizeof(buf), "%s:%s:%s:%s:%s:FILETRANS:\n",
 #ifdef WORDS_BIGENDIAN
                             "BIG",
 #else
                             "LIT",
 #endif
                             mid->username, hash, mid->language,
-                            mid->database == NULL ? "" : mid->database) >= 
BLOCK) {;
+                            mid->database == NULL ? "" : mid->database) >= 
(int) sizeof(buf)) {;
                        mapi_setError(mid, "combination of database name and 
user name too long", "mapi_reconnect", MERROR);
                        free(hash);
                        close_connection(mid);
@@ -2701,14 +2696,14 @@ mapi_reconnect(Mapi mid)
                /* because the headers changed, and because it makes no sense to
                 * try and be backwards (or forwards) compatible, we bail out
                 * with a friendly message saying so */
-               snprintf(buf, BLOCK, "unsupported protocol version: %d, "
+               snprintf(buf, sizeof(buf), "unsupported protocol version: %d, "
                         "this client only supports version 9", pversion);
                mapi_setError(mid, buf, "mapi_reconnect", MERROR);
                close_connection(mid);
                return mid->error;
        }
        if (mid->trace) {
-               printf("sending first request [%d]:%s", BLOCK, buf);
+               printf("sending first request [%zu]:%s", sizeof(buf), buf);
                fflush(stdout);
        }
        len = strlen(buf);
diff --git a/common/stream/stream.c b/common/stream/stream.c
--- a/common/stream/stream.c
+++ b/common/stream/stream.c
@@ -3720,7 +3720,7 @@ bs_write(stream *restrict ss, const void
 {
        bs *s;
        size_t todo = cnt * elmsize;
-       int16_t blksize;
+       uint16_t blksize;
 
        s = (bs *) ss->stream_data.p;
        if (s == NULL)
@@ -3753,13 +3753,13 @@ bs_write(stream *restrict ss, const void
 #endif
                        /* since the block is at max BLOCK (8K) - 2 size we can
                         * store it in a two byte integer */
-                       blksize = (int16_t) s->nr;
+                       blksize = (uint16_t) s->nr;
                        s->bytes += s->nr;
                        /* the last bit tells whether a flush is in
                         * there, it's not at this moment, so shift it
                         * to the left */
-                       blksize = (int16_t) (blksize << 1);
-                       if (!mnstr_writeSht(s->s, blksize) ||
+                       blksize <<= 1;
+                       if (!mnstr_writeSht(s->s, (int16_t) blksize) ||
                            s->s->write(s->s, s->buf, 1, s->nr) != (ssize_t) 
s->nr) {
                                ss->errnr = MNSTR_WRITE_ERROR;
                                return -1;
@@ -3779,7 +3779,7 @@ bs_write(stream *restrict ss, const void
 static int
 bs_flush(stream *ss)
 {
-       int16_t blksize;
+       uint16_t blksize;
        bs *s;
 
        s = (bs *) ss->stream_data.p;
@@ -3804,13 +3804,13 @@ bs_flush(stream *ss)
                        fprintf(stderr, "W %s 0\n", ss->name);
                }
 #endif
-               blksize = (int16_t) (s->nr << 1);
+               blksize = (uint16_t) (s->nr << 1);
                s->bytes += s->nr;
                /* indicate that this is the last buffer of a block by
                 * setting the low-order bit */
-               blksize |= (int16_t) 1;
+               blksize |= 1;
                /* allways flush (even empty blocks) needed for the protocol) */
-               if ((!mnstr_writeSht(s->s, blksize) ||
+               if ((!mnstr_writeSht(s->s, (int16_t) blksize) ||
                     (s->nr > 0 &&
                      s->s->write(s->s, s->buf, 1, s->nr) != (ssize_t) s->nr))) 
{
                        ss->errnr = MNSTR_WRITE_ERROR;
@@ -3870,17 +3870,17 @@ bs_read(stream *restrict ss, void *restr
                case 1:
                        break;
                }
-               if (blksize < 0) {
+               if ((uint16_t) blksize > (BLOCK << 1 | 1)) {
                        ss->errnr = MNSTR_READ_ERROR;
                        return -1;
                }
 #ifdef BSTREAM_DEBUG
-               fprintf(stderr, "RC size: %d, final: %s\n", blksize >> 1, 
blksize & 1 ? "true" : "false");
-               fprintf(stderr, "RC %s %d\n", ss->name, blksize);
+               fprintf(stderr, "RC size: %u, final: %s\n", (uint16_t) blksize 
>> 1, (uint16_t) blksize & 1 ? "true" : "false");
+               fprintf(stderr, "RC %s %u\n", ss->name, (uint16_t) blksize);
 #endif
-               s->itotal = (unsigned) (blksize >> 1);  /* amount readable */
+               s->itotal = (uint16_t) blksize >> 1;    /* amount readable */
                /* store whether this was the last block or not */
-               s->nr = blksize & 1;
+               s->nr = (uint16_t) blksize & 1;
                s->bytes += s->itotal;
                s->blks++;
        }
@@ -3936,18 +3936,18 @@ bs_read(stream *restrict ss, void *restr
                        case 1:
                                break;
                        }
-                       if (blksize < 0) {
+                       if ((uint16_t) blksize > (BLOCK << 1 | 1)) {
                                ss->errnr = MNSTR_READ_ERROR;
                                return -1;
                        }
 #ifdef BSTREAM_DEBUG
-                       fprintf(stderr, "RC size: %d, final: %s\n", blksize >> 
1, blksize & 1 ? "true" : "false");
+                       fprintf(stderr, "RC size: %d, final: %s\n", (uint16_t) 
blksize >> 1, (uint16_t) blksize & 1 ? "true" : "false");
                        fprintf(stderr, "RC %s %d\n", ss->name, s->nr);
                        fprintf(stderr, "RC %s %d\n", ss->name, blksize);
 #endif
-                       s->itotal = (unsigned) (blksize >> 1);  /* amount 
readable */
+                       s->itotal = (uint16_t) blksize >> 1;    /* amount 
readable */
                        /* store whether this was the last block or not */
-                       s->nr = blksize & 1;
+                       s->nr = (uint16_t) blksize & 1;
                        s->bytes += s->itotal;
                        s->blks++;
                }
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to