Changeset: 9c0db3a09331 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/9c0db3a09331
Modified Files:
        clients/Tests/exports.stable.out
        monetdb5/mal/mal_exception.c
        monetdb5/mal/mal_exception.h
        monetdb5/mal/mal_parser.c
Branch: Dec2025
Log Message:

Use new function appendException to concatenate MAL parser exceptions.


diffs (truncated from 1307 to 300 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -840,6 +840,7 @@ const char alter_set_tableRef[];
 const char alter_tableRef[];
 const char alter_userRef[];
 const char appendBulkRef[];
+str appendException(enum malexception, const char *, _In_z_ 
_Printf_format_string_ const char *, ...) __attribute__((__malloc__)) 
__attribute__((__format__(__printf__, 3, 4))) 
__attribute__((__returns_nonnull__));
 const char appendRef[];
 const char assertRef[];
 const char avgRef[];
@@ -891,7 +892,6 @@ char *concatErrors(const char *err1, con
 const char connectRef[];
 const char containsRef[];
 str convertConstant(allocator *ma, malType type, ValPtr vr);
-str copyException(allocator *, const char *);
 InstrPtr copyInstruction(MalBlkPtr mb, const InstrRecord *p);
 InstrPtr copyInstructionArgs(MalBlkPtr mb, const InstrRecord *p, int args);
 MalBlkPtr copyMalBlk(MalBlkPtr mb);
diff --git a/monetdb5/mal/mal_exception.c b/monetdb5/mal/mal_exception.c
--- a/monetdb5/mal/mal_exception.c
+++ b/monetdb5/mal/mal_exception.c
@@ -73,12 +73,13 @@ concatErrors(const char *err1, const cha
  */
 __attribute__((__format__(__printf__, 3, 0), __returns_nonnull__))
 static str
-createExceptionInternal(enum malexception type, const char *fcn,
+createExceptionInternal(bool append, enum malexception type, const char *fcn,
                                                const char *format, va_list ap)
 {
        int len;
        char *msg;
        va_list ap2;
+       size_t buflen = GDKMAXERRLEN;
 
        va_copy(ap2, ap);                       /* we need to use it twice */
        len = vsnprintf(NULL, 0, format, ap);   /* count necessary length */
@@ -88,19 +89,26 @@ createExceptionInternal(enum malexceptio
        }
        msg = MT_thread_get_exceptbuf();
        if (msg != NULL) {
+               if (append) {
+                       size_t mlen = strlen(msg);
+                       msg += mlen;
+                       buflen -= mlen;
+                       if (buflen < 64)
+                               return MT_thread_get_exceptbuf();
+               }
                /* the calls below succeed: the arguments have already been 
checked */
-               size_t msglen = strconcat_len(msg, GDKMAXERRLEN, 
exceptionNames[type],
+               size_t msglen = strconcat_len(msg, buflen, exceptionNames[type],
                                                                          ":", 
fcn, ":", NULL);
-               if (len > 0 && msglen < GDKMAXERRLEN) {
-                       int prlen = vsnprintf(msg + msglen, GDKMAXERRLEN - 
msglen, format, ap2);
-                       if (msglen + prlen >= GDKMAXERRLEN)
-                               strcpy(msg + GDKMAXERRLEN - 5, "...\n");
+               if (len > 0 && msglen < buflen) {
+                       int prlen = vsnprintf(msg + msglen, buflen - msglen, 
format, ap2);
+                       if (msglen + prlen >= buflen)
+                               strcpy(msg + buflen - 5, "...\n");
                }
                char *q = msg + strlen(msg);
                if (q[-1] != '\n') {
                        /* make sure message ends with newline */
-                       if (q >= msg + GDKMAXERRLEN - 1) {
-                               strcpy(msg + GDKMAXERRLEN - 5, "...\n");
+                       if (q >= msg + buflen - 1) {
+                               strcpy(msg + buflen - 5, "...\n");
                        } else {
                                *q++ = '\n';
                                *q = '\0';
@@ -111,6 +119,7 @@ createExceptionInternal(enum malexceptio
                        TRC_ERROR(MAL_SERVER, "%.*s\n", (int) (p - q), q);
                if (*q)
                        TRC_ERROR(MAL_SERVER, "%s\n", q);
+               msg = MT_thread_get_exceptbuf();
        } else {
                msg = M5OutOfMemory;
        }
@@ -171,7 +180,21 @@ createException(enum malexception type, 
                return ret;
        }
        va_start(ap, format);
-       ret = createExceptionInternal(type, fcn, format, ap);
+       ret = createExceptionInternal(false, type, fcn, format, ap);
+       va_end(ap);
+       GDKclrerr();
+
+       assert(ret);
+       return ret;
+}
+
+str
+appendException(enum malexception type, const char *fcn, const char *format,
+                               ...)
+{
+       va_list ap;
+       va_start(ap, format);
+       str ret = createExceptionInternal(true, type, fcn, format, ap);
        va_end(ap);
        GDKclrerr();
 
diff --git a/monetdb5/mal/mal_exception.h b/monetdb5/mal/mal_exception.h
--- a/monetdb5/mal/mal_exception.h
+++ b/monetdb5/mal/mal_exception.h
@@ -52,6 +52,12 @@ mal_export str createException(enum male
        __attribute__((__malloc__(freeException, 1)))
        __attribute__((__format__(__printf__, 3, 4)))
        __attribute__((__returns_nonnull__));
+/* only use immediately after createException */
+mal_export str appendException(enum malexception, const char *,
+                                                          _In_z_ 
_Printf_format_string_ const char *, ...)
+       __attribute__((__malloc__))
+       __attribute__((__format__(__printf__, 3, 4)))
+       __attribute__((__returns_nonnull__));
 /*FIXmal_export str createMalException(MalBlkPtr mb, int pc, enum malexception 
type, const char *prev, const char *format, ...);*/
 mal_export str createMalException(MalBlkPtr, int, enum malexception,
                                                                  _In_z_ 
_Printf_format_string_ const char *,
diff --git a/monetdb5/mal/mal_parser.c b/monetdb5/mal/mal_parser.c
--- a/monetdb5/mal/mal_parser.c
+++ b/monetdb5/mal/mal_parser.c
@@ -76,12 +76,11 @@ skipToEnd(Client ctx)
  * Keep on syntax error for reflection and correction.
  */
 static void
-parseError(allocator *ma, Client ctx, str msg)
+parseError(Client ctx, str msg)
 {
        MalBlkPtr mb;
-       char *old, *new;
        char buf[1028] = { 0 };
-       char *s = buf, *t, *line = "", *marker = "";
+       char *s = buf, *t;
        char *l = lastline(ctx);
        ssize_t i;
 
@@ -98,7 +97,10 @@ parseError(allocator *ma, Client ctx, st
        }
        *s++ = '\n';
        *s = 0;
-       line = createException(SYNTAX, "parseError", "%s", buf);
+       if (mb->errors == MT_thread_get_exceptbuf()) /* implies != NULL */
+               mb->errors = appendException(SYNTAX, "parseError", "%s", buf);
+       else
+               mb->errors = createException(SYNTAX, "parseError", "%s", buf);
 
        /* produce the position marker */
        s = buf;
@@ -108,27 +110,8 @@ parseError(allocator *ma, Client ctx, st
        }
        *s++ = '^';
        *s = 0;
-       marker = createException(SYNTAX, "parseError", "%s%s", buf, msg);
+       mb->errors = appendException(SYNTAX, "parseError", "%s%s", buf, msg);
 
-       old = mb->errors;
-       new = ma_alloc(ma, (old ? strlen(old) : 0) + strlen(line) + 
strlen(marker) +
-                                       64);
-       if (new == NULL) {
-               freeException(line);
-               freeException(marker);
-               skipToEnd(ctx);
-               return;                                 // just stick to old 
error message
-       }
-       mb->errors = new;
-       if (old) {
-               new = stpcpy(new, old);
-               //GDKfree(old);
-       }
-       new = stpcpy(new, line);
-       new = stpcpy(new, marker);
-
-       freeException(line);
-       freeException(marker);
        skipToEnd(ctx);
 }
 
@@ -606,7 +589,7 @@ cstToken(allocator *ma, Client ctx, MalB
                        size_t len = sizeof(flt);
                        float *pval = &cst->val.fval;
                        if (fltFromStr(ma, CURRENT(ctx), &len, &pval, false) < 
0) {
-                               parseError(ma, ctx, GDKerrbuf);
+                               parseError(ctx, GDKerrbuf);
                                return i;
                        }
                }
@@ -614,7 +597,7 @@ cstToken(allocator *ma, Client ctx, MalB
                        size_t len = sizeof(dbl);
                        double *pval = &cst->val.dval;
                        if (dblFromStr(ma, CURRENT(ctx), &len, &pval, false) < 
0) {
-                               parseError(ma, ctx, GDKerrbuf);
+                               parseError(ctx, GDKerrbuf);
                                return i;
                        }
                }
@@ -622,7 +605,7 @@ cstToken(allocator *ma, Client ctx, MalB
                        size_t len = sizeof(lng);
                        lng l, *pval = &l;
                        if (lngFromStr(ma, CURRENT(ctx), &len, &pval, false) < 
0) {
-                               parseError(ma, ctx, GDKerrbuf);
+                               parseError(ctx, GDKerrbuf);
                                return i;
                        }
                        if (is_lng_nil(l) || l < 0
@@ -657,14 +640,14 @@ cstToken(allocator *ma, Client ctx, MalB
                                size_t len = sizeof(dbl);
                                dbl *pval = &cst->val.dval;
                                if (dblFromStr(ma, CURRENT(ctx), &len, &pval, 
false) < 0) {
-                                       parseError(ma, ctx, GDKerrbuf);
+                                       parseError(ctx, GDKerrbuf);
                                        return i;
                                }
                        } else {
                                size_t len = sizeof(lng);
                                lng *pval = &cst->val.lval;
                                if (lngFromStr(ma, CURRENT(ctx), &len, &pval, 
false) < 0) {
-                                       parseError(ma, ctx, GDKerrbuf);
+                                       parseError(ctx, GDKerrbuf);
                                        return i;
                                }
                        }
@@ -682,7 +665,7 @@ cstToken(allocator *ma, Client ctx, MalB
                                s++;
                        }
                        if (hgeFromStr(ma, CURRENT(ctx), &len, &pval, false) < 
0) {
-                               parseError(ma, ctx, GDKerrbuf);
+                               parseError(ctx, GDKerrbuf);
                                return i;
                        }
                        return i;
@@ -772,7 +755,7 @@ cstToken(allocator *ma, Client ctx, MalB
  * encoding tables, or type dependency should be modeled as properties.
  */
 static int
-typeAlias(allocator *ma, Client ctx, int tpe)
+typeAlias(Client ctx, int tpe)
 {
        int t;
 
@@ -782,7 +765,7 @@ typeAlias(allocator *ma, Client ctx, int
                nextChar(ctx);
                t = currChar(ctx) - '0';
                if (t <= 0 || t > 3) {
-                       parseError(ma, ctx, "[1-3] expected\n");
+                       parseError(ctx, "[1-3] expected\n");
                        return -1;
                } else
                        nextChar(ctx);
@@ -796,7 +779,7 @@ typeAlias(allocator *ma, Client ctx, int
  * We should change getMALtype to return a failure instead.
  */
 static int
-simpleTypeId(allocator *ma, Client ctx)
+simpleTypeId(Client ctx)
 {
        int tpe;
        size_t l;
@@ -804,7 +787,7 @@ simpleTypeId(allocator *ma, Client ctx)
        nextChar(ctx);
        l = typeidLength(ctx);
        if (l == 0) {
-               parseError(ma, ctx, "Type identifier expected\n");
+               parseError(ctx, "Type identifier expected\n");
                ctx->yycur--;                   /* keep it */
                return -1;
        }
@@ -813,7 +796,7 @@ simpleTypeId(allocator *ma, Client ctx)
        else
                tpe = getAtomIndex(CURRENT(ctx), l, -1);
        if (tpe < 0) {
-               parseError(ma, ctx, "Type identifier expected\n");
+               parseError(ctx, "Type identifier expected\n");
                ctx->yycur -= l;                /* keep it */
                return TYPE_void;
        }
@@ -822,7 +805,7 @@ simpleTypeId(allocator *ma, Client ctx)
 }
 
 static int
-parseTypeId(allocator *ma, Client ctx)
+parseTypeId(Client ctx)
 {
        int i = TYPE_any, kt = 0;
        char *s = CURRENT(ctx);
@@ -845,17 +828,17 @@ parseTypeId(allocator *ma, Client ctx)
                        if (!opt)
                                return newBatType(TYPE_any);
 
-                       parseError(ma, ctx, "':bat[:type]' expected\n");
+                       parseError(ctx, "':bat[:type]' expected\n");
                        return -1;
                }
                advance(ctx, 1);
                if (currChar(ctx) == ':') {
-                       tt = simpleTypeId(ma, ctx);
-                       kt = typeAlias(ma, ctx, tt);
+                       tt = simpleTypeId(ctx);
+                       kt = typeAlias(ctx, tt);
                        if (kt < 0)
                                return kt;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to