Changeset: cb696b854f94 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/cb696b854f94
Modified Files:
        clients/Tests/exports.stable.out
        monetdb5/mal/mal_exception.c
        monetdb5/mal/mal_function.c
        monetdb5/mal/mal_instruction.c
        monetdb5/mal/mal_instruction.h
        monetdb5/mal/mal_parser.c
        monetdb5/mal/mal_session.c
        monetdb5/optimizer/opt_evaluate.c
Branch: Dec2025
Log Message:

Use exception buffer also for MAL parsing errors.


diffs (truncated from 496 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
@@ -824,7 +824,7 @@ int TRACEtable(Client cntxt, BAT **r);
 int TYPE_xml;
 int UTF8_strlen(const char *s);
 int UTF8_strwidth(const char *s);
-void addMalException(MalBlkPtr mb, str msg);
+void addMalException(MalBlkPtr mb, const char *msg);
 str addOptimizerPipe(Client cntxt, MalBlkPtr mb, const char *name);
 str addPipeDefinition(Client cntxt, const char *name, const char *pipe);
 const char affectedRowsRef[];
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
@@ -218,54 +218,50 @@ freeException(str msg)
 __attribute__((__format__(__printf__, 5, 0), __returns_nonnull__))
 static str
 createMalExceptionInternal(MalBlkPtr mb, int pc, enum malexception type,
-                                                  char *prev, const char 
*format, va_list ap)
+                                                  const char *prev, const char 
*format, va_list ap)
 {
        bool addnl = false;
-       const char *s = mb && getInstrPtr(mb, 0) ? getModName(mb) : "unknown";
-       const char *fcn = mb && getInstrPtr(mb, 0) ? getFcnName(mb) : "unknown";
-       size_t msglen;
+       const char *mod = getInstrPtr(mb, 0) ? getModName(mb) : "unknown";
+       const char *fcn = getInstrPtr(mb, 0) ? getFcnName(mb) : "unknown";
+       char *buf = MT_thread_get_exceptbuf();
+       size_t buflen = GDKMAXERRLEN;
 
        if (prev) {
-               msglen = strlen(prev);
+               size_t msglen = strlen(prev);
+               assert(msglen < buflen);
+               if (prev != buf) {
+                       strcpy_len(buf, prev, buflen);
+               }
+               buf += msglen;
+               buflen -= msglen;
                if (msglen > 0 && prev[msglen - 1] != '\n') {
                        addnl = true;
                        msglen++;
                }
-               msglen += snprintf(NULL, 0, "!%s:%s.%s[%d]:",
-                                                  exceptionNames[type], s, 
fcn, pc);
-       } else if (type == SYNTAX) {
-               msglen = strlen(exceptionNames[type]) + 1;
-       } else {
-               msglen = snprintf(NULL, 0, "%s:%s.%s[%d]:",
-                                                 exceptionNames[type], s, fcn, 
pc);
        }
-       va_list ap2;
-       va_copy(ap2, ap);
-       int len = vsnprintf(NULL, 0, format, ap);
-       if (len < 0)
-               len = 0;
-       char *msg = ma_alloc(mb->ma, msglen + len + 1);
-       if (msg != NULL) {
-               /* the calls below succeed: the arguments have already been 
checked */
-               if (prev) {
-                       (void) snprintf(msg, msglen + 1, "%s%s!%s:%s.%s[%d]:",
-                                                       prev, addnl ? "\n" : "",
-                                                       exceptionNames[type], 
s, fcn, pc);
-               } else if (type == SYNTAX) {
-                       (void) strconcat_len(msg, msglen + 1,
-                                                                
exceptionNames[type], ":", NULL);
+       if (type == SYNTAX) {
+               size_t msglen = strconcat_len(buf, buflen,
+                                                                         
exceptionNames[type], ":", NULL);
+               if (msglen < buflen) {
+                       buf += msglen;
+                       buflen -= msglen;
                } else {
-                       (void) snprintf(msg, msglen + 1, "%s:%s.%s[%d]:",
-                                                       exceptionNames[type], 
s, fcn, pc);
+                       buflen = 0;
                }
-               if (len > 0)
-                       (void) vsnprintf(msg + msglen, len + 1, format, ap2);
        } else {
-               msg = M5OutOfMemory;
+               int msglen = snprintf(buf, buflen, "%s!%s:%s.%s[%d]:",
+                                                         addnl ? "\n" : "",
+                                                         exceptionNames[type], 
mod, fcn, pc);
+               if ((size_t) msglen < buflen) {
+                       buf += msglen;
+                       buflen -= (size_t) msglen;
+               } else {
+                       buflen = 0;
+               }
        }
-       va_end(ap2);
-       freeException(prev);
-       return msg;
+       if (buflen > 0)
+               (void) vsnprintf(buf, buflen, format, ap);
+       return MT_thread_get_exceptbuf();
 }
 
 /**
diff --git a/monetdb5/mal/mal_function.c b/monetdb5/mal/mal_function.c
--- a/monetdb5/mal/mal_function.c
+++ b/monetdb5/mal/mal_function.c
@@ -373,7 +373,7 @@ cloneFunction(Module scope, Symbol proc,
 
        /* check for errors after fixation , TODO */
        /* beware, we should now ignore any cloning */
-       if (proc->def->errors == 0) {
+       if (proc->def->errors == NULL) {
                msg = chkProgram(scope, new->def);
                if (msg)
                        mb->errors = msg;
@@ -381,7 +381,7 @@ cloneFunction(Module scope, Symbol proc,
                        assert(mb->errors == NULL);
                        mb->errors = new->def->errors;
                        mb->errors = createMalException(mb, 0, TYPE, "Error in 
cloned function");
-                       new->def->errors = 0;
+                       new->def->errors = NULL;
                }
        }
 
diff --git a/monetdb5/mal/mal_instruction.c b/monetdb5/mal/mal_instruction.c
--- a/monetdb5/mal/mal_instruction.c
+++ b/monetdb5/mal/mal_instruction.c
@@ -28,15 +28,18 @@
  * for the upper layers to abandon the track
  */
 void
-addMalException(MalBlkPtr mb, str msg)
+addMalException(MalBlkPtr mb, const char *msg)
 {
        if (msg == NULL)
                return;
+       size_t len;
        if (mb->errors) {
-               mb->errors = concatErrors(mb->errors, msg);
+               len = strlen(mb->errors);
        } else {
-               mb->errors = ma_strdup(mb->ma, msg);
+               mb->errors = MT_thread_get_exceptbuf();
+               len = 0;
        }
+       strcpy_len(mb->errors + len, msg, GDKMAXERRLEN - len);
 }
 
 Symbol
@@ -386,7 +389,7 @@ copyMalBlk(MalBlkPtr old)
        }
 
        strcpy_len(mb->binding, old->binding, sizeof(mb->binding));
-       mb->errors = old->errors ? ma_strdup(mb->ma, old->errors) : 0;
+       mb->errors = old->errors;       /* WHY copy errors? */
        mb->tag = old->tag;
        mb->runtime = old->runtime;
        mb->calls = old->calls;
diff --git a/monetdb5/mal/mal_instruction.h b/monetdb5/mal/mal_instruction.h
--- a/monetdb5/mal/mal_instruction.h
+++ b/monetdb5/mal/mal_instruction.h
@@ -142,7 +142,7 @@ mal_export char *getVarNameIntoBuffer(Ma
 #define getArgGDKType(M,P,I) getVarGDKType((M),(P)->argv[I])
 #define getGDKType(T)          ((T) <= TYPE_str ? (T) : ((T) == TYPE_any ? 
TYPE_void : findGDKtype(T)))
 
-mal_export void addMalException(MalBlkPtr mb, str msg);
+mal_export void addMalException(MalBlkPtr mb, const char *msg);
 mal_export void mal_instruction_reset(void);
 mal_export InstrPtr newInstruction(MalBlkPtr mb, const char *modnme,
                                                                   const char 
*fcnnme);
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,7 +76,7 @@ skipToEnd(Client ctx)
  * Keep on syntax error for reflection and correction.
  */
 static void
-parseError(Client ctx, str msg)
+parseError(Client ctx, const char *msg)
 {
        MalBlkPtr mb;
        char buf[1028] = { 0 };
@@ -97,10 +97,11 @@ parseError(Client ctx, str msg)
        }
        *s++ = '\n';
        *s = 0;
-       if (mb->errors == MT_thread_get_exceptbuf()) /* implies != NULL */
-               mb->errors = appendException(SYNTAX, "parseError", "%s", buf);
+       assert(mb->errors == NULL || mb->errors == MT_thread_get_exceptbuf());
+       if (mb->errors)
+               (void) appendException(SYNTAX, "parseError", "%s", buf);
        else
-               mb->errors = createException(SYNTAX, "parseError", "%s", buf);
+               (void) createException(SYNTAX, "parseError", "%s", buf);
 
        /* produce the position marker */
        s = buf;
@@ -1111,8 +1112,6 @@ parseAtom(Client ctx)
        else
                tpe = parseTypeId(ctx);
        if (ATOMindex(modnme) < 0) {
-               if (ctx->curprg->def->errors)
-                       freeException(ctx->curprg->def->errors);
                ctx->curprg->def->errors = malAtomDefinition(modnme, tpe);
        }
        if (modnme != userRef)
@@ -1680,7 +1679,7 @@ fcnHeader(Client ctx, int kind)
                return 0;
        }
        ctx->curprg->def->errors = ctx->backup->def->errors;
-       ctx->backup->def->errors = 0;
+       ctx->backup->def->errors = NULL;
        curPrg = ctx->curprg;
        curBlk = curPrg->def;
        curInstr = getInstrPtr(curBlk, 0);
@@ -1842,8 +1841,10 @@ parseEnd(Client ctx)
        Symbol curPrg = 0;
        size_t l;
        InstrPtr sig;
-       str errors = MAL_SUCCEED, msg = MAL_SUCCEED;
+       str errors = MAL_SUCCEED;
 
+       if (ctx->curprg->def->errors)
+               return 1;
        if (MALkeyword(ctx, "end", 3)) {
                curPrg = ctx->curprg;
                l = idLength(ctx);
@@ -1873,36 +1874,10 @@ parseEnd(Client ctx)
                else
                        insertSymbol(getModule(getModuleId(sig)), ctx->curprg);
 
-               if (ctx->curprg->def->errors) {
-                       errors = ctx->curprg->def->errors;
-                       ctx->curprg->def->errors = 0;
-               }
-               // check for newly identified errors
-               msg = chkProgram(ctx->usermodule, ctx->curprg->def);
-               if (errors == NULL)
-                       errors = msg;
-               else
-                       freeException(msg);
-               if (errors == NULL) {
-                       errors = ctx->curprg->def->errors;
-                       ctx->curprg->def->errors = 0;
-               } else if (ctx->curprg->def->errors) {
-                       //collect all errors for reporting
-                       str new = GDKmalloc(strlen(errors) +
-                                                               
strlen(ctx->curprg->def->errors) + 16);
-                       if (new) {
-                               char *p = stpcpy(new, errors);
-                               if (p[-1] != '\n')
-                                       *p++ = '\n';
-                               *p++ = '!';
-                               strcpy(p, ctx->curprg->def->errors);
-
-                               freeException(errors);
-                               freeException(ctx->curprg->def->errors);
-
-                               ctx->curprg->def->errors = 0;
-                               errors = new;
-                       }
+               errors = chkProgram(ctx->usermodule, ctx->curprg->def);
+               if (errors) {
+                       ctx->curprg->def->errors = errors;
+                       return 1;
                }
 
                if (ctx->backup) {
@@ -1912,28 +1887,9 @@ parseEnd(Client ctx)
                        str msg;
                        if ((msg = MSinitClientPrg(ctx, ctx->curmodule->name,
                                                                           
mainRef)) != MAL_SUCCEED) {
-                               if (errors) {
-                                       str new = GDKmalloc(strlen(errors) + 
strlen(msg) + 3);
-                                       if (new) {
-                                               char *p = stpcpy(new, msg);
-                                               if (p[-1] != '\n')
-                                                       *p++ = '\n';
-                                               strcpy(p, errors);
-                                               freeException(errors);
-                                               ctx->curprg->def->errors = new;
-                                       } else {
-                                               ctx->curprg->def->errors = 
errors;
-                                       }
-                                       freeException(msg);
-                               } else {
-                                       ctx->curprg->def->errors = msg;
-                               }
-                               return 1;
+                               ctx->curprg->def->errors = msg;
                        }
                }
-               // pass collected errors to context
-               assert(ctx->curprg->def->errors == NULL);
-               ctx->curprg->def->errors = errors;
                return 1;
        }
        return 0;
@@ -2314,20 +2270,20 @@ parseMAL(Client ctx, Symbol curPrg, int 
                                pushInstruction(curBlk, curInstr);
                        }
                        echoInput(ctx);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to