Changeset: 29e407599343 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/29e407599343
Modified Files:
        clients/examples/C/streamcat.c
        clients/mapiclient/msqldump.c
        clients/mapilib/mapi.c
        monetdb5/modules/mal/tablet.c
        sql/backends/monet5/sql_result.c
        sql/storage/store.c
        tools/merovingian/daemon/snapshot.c
Branch: default
Log Message:

Avoid malloc/free where we can by using mnstr_peek_error instead of mnstr_error.
Also fix some memory leaks right before exit ;-) .


diffs (208 lines):

diff --git a/clients/examples/C/streamcat.c b/clients/examples/C/streamcat.c
--- a/clients/examples/C/streamcat.c
+++ b/clients/examples/C/streamcat.c
@@ -164,7 +164,7 @@ int cmd_read(char *argv[])
 
        s = opener(filename);
        if (s == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR) {
-               char *msg = mnstr_error(s);
+               const char *msg = mnstr_peek_error(s);
                croak(2, "Opener %s failed: %s", opener_name, msg ? msg : "<no 
error message>");
        }
 
@@ -200,7 +200,7 @@ int cmd_read(char *argv[])
                if (wrapper != NULL)
                        s = wrapper(s, parms);
                if (s == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR) {
-                       char *msg = mnstr_error(s);
+                       const char *msg = mnstr_peek_error(s);
                        croak(2, "Opener %s failed: %s", opener_name, msg ? msg 
: "<no error message>");
                }
        }
@@ -269,7 +269,7 @@ int cmd_write(char *argv[])
 
        s = opener(filename);
        if (s == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR) {
-               char *msg = mnstr_error(NULL);
+               const char *msg = mnstr_peek_error(NULL);
                croak(2, "Opener %s failed: %s", opener_name, msg ? msg : "");
        }
 
@@ -313,7 +313,7 @@ int cmd_write(char *argv[])
                if (wrapper != NULL)
                        s = wrapper(s, parms);
                if (s == NULL || mnstr_errnr(s) != MNSTR_NO__ERROR) {
-                       char *msg = mnstr_error(s);
+                       const char *msg = mnstr_peek_error(s);
                        croak(2, "Opener %s failed: %s", opener_name, msg ? msg 
: "<no error message>");
                }
        }
@@ -350,7 +350,7 @@ static void copy_stream_to_file(stream *
                iterations += 1;
                nread = mnstr_read(in, buffer, 1, bufsize);
                if (nread < 0)
-                       croak(2, "Error reading from stream after %" PRIu64 " 
bytes: %s", total, mnstr_error(in));
+                       croak(2, "Error reading from stream after %" PRIu64 " 
bytes: %s", total, mnstr_peek_error(in));
                if (nread == 0) {
                        // eof
                        break;
@@ -395,15 +395,15 @@ static void copy_file_to_stream(FILE *in
                }
                nwritten = mnstr_write(out, buffer, 1, nread);
                if (nwritten < 0)
-                       croak(2, "Write error after %" PRId64 " bytes: %s", 
total, mnstr_error(out));
+                       croak(2, "Write error after %" PRId64 " bytes: %s", 
total, mnstr_peek_error(out));
                if ((size_t)nwritten != nread)
                        croak(2, "Partial write (%lu/%lu bytes) after %" PRId64 
" bytes: %s",
                                (unsigned long)nwritten,  (unsigned long)nread,
-                               total + (int64_t)nwritten, mnstr_error(out));
+                               total + (int64_t)nwritten, 
mnstr_peek_error(out));
                total += (int64_t)nwritten;
                if (do_flush)
                        if (mnstr_flush(out, flush_level) != 0)
-                               croak(2, "Flush failed after %" PRId64 " bytes: 
%s", total, mnstr_error(out));
+                               croak(2, "Flush failed after %" PRId64 " bytes: 
%s", total, mnstr_peek_error(out));
        }
 
        free(buffer);
diff --git a/clients/mapiclient/msqldump.c b/clients/mapiclient/msqldump.c
--- a/clients/mapiclient/msqldump.c
+++ b/clients/mapiclient/msqldump.c
@@ -276,9 +276,7 @@ main(int argc, char **argv)
 
        mapi_destroy(mid);
        if (mnstr_errnr(out) != MNSTR_NO__ERROR) {
-               char *err = mnstr_error(out);
-               fprintf(stderr, "%s: %s\n", argv[0], err);
-               free(err);
+               fprintf(stderr, "%s: %s\n", argv[0], mnstr_peek_error(out));
                return 1;
        }
 
diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -1588,9 +1588,7 @@ close_result(MapiHdl hdl)
                                if (mnstr_printf(mid->to, "%s", msg) < 0 ||
                                    mnstr_flush(mid->to, MNSTR_FLUSH_DATA)) {
                                        close_connection(mid);
-                                       char *err = mnstr_error(mid->to);
-                                       mapi_setError(mid, err, __func__, 
MTIMEOUT);
-                                       free(err);
+                                       mapi_setError(mid, 
mnstr_peek_error(mid->to), __func__, MTIMEOUT);
                                        break;
                                }
                                read_into_cache(hdl, 0);
@@ -1608,9 +1606,7 @@ close_result(MapiHdl hdl)
                                if (mnstr_printf(mid->to, "%s", msg) < 0 ||
                                    mnstr_flush(mid->to, MNSTR_FLUSH_DATA)) {
                                        close_connection(mid);
-                                       char *err = mnstr_error(mid->to);
-                                       mapi_setError(mid, err, __func__, 
MTIMEOUT);
-                                       free(err);
+                                       mapi_setError(mid, 
mnstr_peek_error(mid->to), __func__, MTIMEOUT);
                                } else
                                        read_into_cache(hdl, 0);
                        }
@@ -1834,9 +1830,7 @@ finish_handle(MapiHdl hdl)
                        if (mnstr_printf(mid->to, "%s", msg) < 0 ||
                            mnstr_flush(mid->to, MNSTR_FLUSH_DATA)) {
                                close_connection(mid);
-                               char *err = mnstr_error(mid->to);
-                               mapi_setError(mid, err, __func__, MTIMEOUT);
-                               free(err);
+                               mapi_setError(mid, mnstr_peek_error(mid->to), 
__func__, MTIMEOUT);
                                break;
                        }
                        read_into_cache(hdl, 0);
@@ -3428,9 +3422,7 @@ mapi_Xcommand(Mapi mid, const char *cmdn
        if (mnstr_printf(mid->to, "X" "%s %s\n", cmdname, cmdvalue) < 0 ||
            mnstr_flush(mid->to, MNSTR_FLUSH_DATA)) {
                close_connection(mid);
-               char *err = mnstr_error(mid->to);
-               mapi_setError(mid, err, __func__, MTIMEOUT);
-               free(err);
+               mapi_setError(mid, mnstr_peek_error(mid->to), __func__, 
MTIMEOUT);
                return MERROR;
        }
        if (mid->tracelog) {
@@ -4740,9 +4732,7 @@ mapi_cache_limit(Mapi mid, int limit)
                if (mnstr_printf(mid->to, "X" "reply_size %d\n", limit) < 0 ||
                    mnstr_flush(mid->to, MNSTR_FLUSH_DATA)) {
                        close_connection(mid);
-                       char *err = mnstr_error(mid->to);
-                       mapi_setError(mid, err, __func__, MTIMEOUT);
-                       free(err);
+                       mapi_setError(mid, mnstr_peek_error(mid->to), __func__, 
MTIMEOUT);
                        return MERROR;
                }
                hdl = prepareQuery(mapi_new_handle(mid), "reply_size");
diff --git a/monetdb5/modules/mal/tablet.c b/monetdb5/modules/mal/tablet.c
--- a/monetdb5/modules/mal/tablet.c
+++ b/monetdb5/modules/mal/tablet.c
@@ -249,10 +249,9 @@ tablet_skip_string(char *s, char quote, 
 static int
 TABLET_error(stream *s)
 {
-       char *err = mnstr_error(s);
-       /* use free as stream allocates outside GDK */
+       const char *err = mnstr_peek_error(s);
        if (err)
-               free(err);
+               TRC_ERROR(MAL_SERVER, "Stream error: %s\n", err);
        return -1;
 }
 
diff --git a/sql/backends/monet5/sql_result.c b/sql/backends/monet5/sql_result.c
--- a/sql/backends/monet5/sql_result.c
+++ b/sql/backends/monet5/sql_result.c
@@ -541,9 +541,8 @@ mvc_import_table(Client cntxt, BAT ***ba
                throw(IO, "sql.copy_from", SQLSTATE(42000) "No stream (pointer) 
provided");
        if (mnstr_errnr(bs->s) != MNSTR_NO__ERROR) {
                mnstr_error_kind errnr = mnstr_errnr(bs->s);
-               char *stream_msg = mnstr_error(bs->s);
+               const char *stream_msg = mnstr_peek_error(bs->s);
                msg = createException(IO, "sql.copy_from", SQLSTATE(42000) 
"Stream not open %s: %s", mnstr_error_kind_name(errnr), stream_msg ? stream_msg 
: "unknown error");
-               free(stream_msg);
                return msg;
        }
        if (offset < 0 || offset > (lng) BUN_MAX)
diff --git a/sql/storage/store.c b/sql/storage/store.c
--- a/sql/storage/store.c
+++ b/sql/storage/store.c
@@ -2445,9 +2445,7 @@ tar_write_header(stream *tarfile, const 
        tar_write_header_field(&chksum, 8, "%06o", sum);
 
        if (mnstr_write(tarfile, buf, TAR_BLOCK_SIZE, 1) != 1) {
-               char *err = mnstr_error(tarfile);
-               GDKerror("error writing tar header %s: %s", path, err);
-               free(err);
+               GDKerror("error writing tar header %s: %s", path, 
mnstr_peek_error(tarfile));
                return GDK_FAIL;
        }
 
@@ -2530,9 +2528,7 @@ tar_copy_stream(stream *tarfile, const c
                ssize_t chunk = (to_read <= bufsize) ? to_read : bufsize;
                ssize_t nbytes = mnstr_read(contents, buf, 1, chunk);
                if (nbytes != chunk) {
-                       char *err = mnstr_error(contents);
-                       GDKerror("Read only %zd/%zd bytes of component %s: %s", 
nbytes, chunk, path, err);
-                       free(err);
+                       GDKerror("Read only %zd/%zd bytes of component %s: %s", 
nbytes, chunk, path, mnstr_peek_error(contents));
                        goto end;
                }
                ret = tar_write(tarfile, buf, chunk);
diff --git a/tools/merovingian/daemon/snapshot.c 
b/tools/merovingian/daemon/snapshot.c
--- a/tools/merovingian/daemon/snapshot.c
+++ b/tools/merovingian/daemon/snapshot.c
@@ -744,9 +744,7 @@ read_tar_block(stream *s, char *block, e
        if (nread <= 0) {
                if (mnstr_errnr(s) != MNSTR_NO__ERROR) {
                        /* failure */
-                       char *err = mnstr_error(s);
-                       *error = newErr("Read error (%zd): %s", nread, err);
-                       free(err);
+                       *error = newErr("Read error (%zd): %s", nread, 
mnstr_peek_error(s));
                } else {
                        *error = NULL;
                }
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to