Changeset: 9b3d30af4f4d for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=9b3d30af4f4d
Modified Files:
        monetdb5/mal/mal_dataflow.c
        monetdb5/mal/mal_debugger.c
        monetdb5/mal/mal_function.c
        monetdb5/mal/mal_import.c
        monetdb5/mal/mal_interpreter.c
        monetdb5/mal/mal_listing.c
        monetdb5/mal/mal_module.c
        monetdb5/mal/mal_parser.c
        monetdb5/mal/mal_profiler.c
        monetdb5/mal/mal_resolve.c
        monetdb5/mal/mal_session.c
        monetdb5/mal/mal_stack.h
        monetdb5/modules/atoms/blob.c
        monetdb5/modules/atoms/color.c
        monetdb5/modules/atoms/identifier.c
        monetdb5/modules/atoms/inet.c
        monetdb5/modules/atoms/json.c
        monetdb5/modules/atoms/mtime.c
        monetdb5/modules/atoms/streams.c
        monetdb5/modules/kernel/algebra.c
        monetdb5/modules/mal/inspect.c
        monetdb5/modules/mal/mal_mapi.c
        monetdb5/modules/mal/tablet.c
        monetdb5/modules/mal/tokenizer.c
        sql/backends/monet5/sql_gencode.c
        sql/backends/monet5/sql_scenario.c
Branch: Jun2016
Log Message:

More defense lines against failing malloc


diffs (truncated from 817 to 300 lines):

diff --git a/monetdb5/mal/mal_dataflow.c b/monetdb5/mal/mal_dataflow.c
--- a/monetdb5/mal/mal_dataflow.c
+++ b/monetdb5/mal/mal_dataflow.c
@@ -336,7 +336,11 @@ DFLOWworker(void *T)
        srand((unsigned int) GDKusec());
 #endif
        GDKsetbuf(GDKmalloc(GDKMAXERRLEN)); /* where to leave errors */
-       GDKerrbuf[0] = 0;
+       if( GDKerrbuf == 0){
+               fprintf(stderr,"DFLOWworker:Could not allocate GDKerrbuf\n");
+               GDKsetbuf(0);
+       } else
+               GDKerrbuf[0] = 0;
        MT_lock_set(&dataflowLock);
        cntxt = t->cntxt;
        MT_lock_unset(&dataflowLock);
diff --git a/monetdb5/mal/mal_debugger.c b/monetdb5/mal/mal_debugger.c
--- a/monetdb5/mal/mal_debugger.c
+++ b/monetdb5/mal/mal_debugger.c
@@ -272,7 +272,7 @@ printCall(Client cntxt, MalBlkPtr mb, Ma
 {
        str msg;
        msg = instruction2str(mb, stk, getInstrPtr(mb, pc), LIST_MAL_CALL);
-       mnstr_printf(cntxt->fdout, "#%s at %s.%s[%d]\n", msg,
+       mnstr_printf(cntxt->fdout, "#%s at %s.%s[%d]\n", (msg?msg:"failed 
instruction2str()") ,
                        getModuleId(getInstrPtr(mb, 0)),
                        getFunctionId(getInstrPtr(mb, 0)), pc);
        GDKfree(msg);
@@ -287,7 +287,7 @@ printTraceCall(stream *out, MalBlkPtr mb
 
        p = getInstrPtr(mb, pc);
        msg = instruction2str(mb, stk, p, flags);
-       mnstr_printf(out, "#%s%s\n", (mb->errors ? "!" : ""), msg);
+       mnstr_printf(out, "#%s%s\n", (mb->errors ? "!" : ""), msg?msg:"failed 
instruction2str()");
        GDKfree(msg);
 }
 
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
@@ -386,6 +386,10 @@ cloneFunction(stream *out, Module scope,
        printInstruction(out,mb,0,p,LIST_MAL_ALL);
 #endif
        new = newFunction(scope->name, proc->name, getSignature(proc)->token);
+       if( new == NULL){
+               fprintf(stderr,"cloneFunction() failed");
+               return NULL;
+       }
        freeMalBlk(new->def);
        new->def = copyMalBlk(proc->def);
        /* now change the definition of the original proc */
@@ -486,7 +490,7 @@ debugFunction(stream *fd, MalBlkPtr mb, 
                                mnstr_printf(fd,"\n");
                        }
                        GDKfree(ps);
-               }
+               } else mnstr_printf(fd,"#failed instruction2str()\n");
        }
 }
 
@@ -517,7 +521,7 @@ listFunction(stream *fd, MalBlkPtr mb, M
                                if (l > len)
                                        len = l;
                                GDKfree(ps);
-                       }
+                       } else mnstr_printf(fd,"#failed instruction2str()\n");
                }
                mnstr_printf(fd, "%% " SZFMT " # length\n", len);
        }
diff --git a/monetdb5/mal/mal_import.c b/monetdb5/mal/mal_import.c
--- a/monetdb5/mal/mal_import.c
+++ b/monetdb5/mal/mal_import.c
@@ -290,8 +290,10 @@ evalFile(Client c, str fname, int listin
                        c->yycur = 0;
                        c->bak = NULL;
                        MSinitClientPrg(c, "user", "main");     /* 
re-initialize context */
-                       MCpushClientInput(c, bstream_create(fd, 128 * BLOCK), 
c->listing, "");
-                       msg = runScenario(c);
+                       if( MCpushClientInput(c, bstream_create(fd, 128 * 
BLOCK), c->listing, "") < 0){
+                               msg = createException(MAL,"mal.eval", "WARNING: 
could not switch client input stream\n");
+                       } else
+                               msg = runScenario(c);
                        if (msg != MAL_SUCCEED) {
                                dumpExceptionsToStream(c->fdout, msg);
                                GDKfree(msg);
@@ -309,8 +311,10 @@ evalFile(Client c, str fname, int listin
                c->yycur = 0;
                c->bak = NULL;
                MSinitClientPrg(c, "user", "main");     /* re-initialize 
context */
-               MCpushClientInput(c, bstream_create(fd, 128 * BLOCK), 
c->listing, "");
-               msg = runScenario(c);
+               if( MCpushClientInput(c, bstream_create(fd, 128 * BLOCK), 
c->listing, "") < 0){
+                               msg = createException(MAL,"mal.eval", "WARNING: 
could not switch client input stream\n");
+               } else
+                       msg = runScenario(c);
        }
        GDKfree(fname);
 
diff --git a/monetdb5/mal/mal_interpreter.c b/monetdb5/mal/mal_interpreter.c
--- a/monetdb5/mal/mal_interpreter.c
+++ b/monetdb5/mal/mal_interpreter.c
@@ -482,7 +482,13 @@ str runMALsequence(Client cntxt, MalBlkP
                pci = getInstrPtr(mb, startpc);
                if (pci->argc > 16) {
                        backup = GDKzalloc(pci->argc * sizeof(ValRecord));
+                       if( backup == NULL)
+                               throw(MAL,"mal.interpreter",MAL_MALLOC_FAIL);
                        garbage = (int*)GDKzalloc(pci->argc * sizeof(int));
+                       if( garbage == NULL){
+                               GDKfree(backup);
+                               throw(MAL,"mal.interpreter",MAL_MALLOC_FAIL);
+                       }
                } else {
                        backup = backups;
                        garbage = garbages;
@@ -490,7 +496,13 @@ str runMALsequence(Client cntxt, MalBlkP
                }
        } else if ( mb->maxarg > 16 ){
                backup = GDKzalloc(mb->maxarg * sizeof(ValRecord));
+               if( backup == NULL)
+                       throw(MAL,"mal.interpreter",MAL_MALLOC_FAIL);
                garbage = (int*)GDKzalloc(mb->maxarg * sizeof(int));
+               if( garbage == NULL){
+                       GDKfree(backup);
+                       throw(MAL,"mal.interpreter",MAL_MALLOC_FAIL);
+               }
        } else {
                backup = backups;
                garbage = garbages;
@@ -1344,12 +1356,15 @@ str catchKernelException(Client cntxt, s
                                strcpy(z, ret);
                                if (z[strlen(z) - 1] != '\n') strcat(z, "\n");
                                strcat(z, errbuf);
-                       }
+                       } else // when malloc fails, leave it somewhere
+                               
fprintf(stderr,"!catchKernelException:%s\n",ret);
                } else {
                        /* trap hidden (GDK) exception */
                        z = (char*)GDKmalloc(strlen("GDKerror:") + 
strlen(errbuf) + 2);
                        if (z)
                                sprintf(z, "GDKerror:%s", errbuf);
+                       else
+                               
fprintf(stderr,"!catchKernelException:GDKerror:%s\n",errbuf);
                }
                /* did we eat the error away of not */
                if (z)
diff --git a/monetdb5/mal/mal_listing.c b/monetdb5/mal/mal_listing.c
--- a/monetdb5/mal/mal_listing.c
+++ b/monetdb5/mal/mal_listing.c
@@ -46,6 +46,8 @@ renderTerm(MalBlkPtr mb, MalStkPtr stk, 
        int varid = getArg(p,idx);
 
        buf = GDKzalloc(maxlen);
+       if( buf == NULL)
+               throw(MAL,"renderTerm",MAL_MALLOC_FAIL);
        // show the name when required or is used
        if ((flg & LIST_MAL_NAME) && !isVarConstant(mb,varid) && 
!isVarTypedef(mb,varid)) {
                nme = getVarName(mb,varid);
@@ -526,18 +528,20 @@ mal2str(MalBlkPtr mb, int first, int las
                        totlen += len[i] = (int)strlen(txt[i]);
        }
        ps = GDKmalloc(totlen + mb->stop + 1);
-       if( ps == NULL)
+       if( ps == NULL){
                GDKerror("mal2str: " MAL_MALLOC_FAIL);
+               GDKfree(len);
+               GDKfree(txt);
+               return NULL;
+       }
 
        totlen = 0;
        for (i = first; i < last; i++) {
                if( txt[i]){
-                       if( ps){
-                               strncpy(ps + totlen, txt[i], len[i]);
-                               ps[totlen + len[i]] = '\n';
-                               ps[totlen + len[i] + 1] = 0;
-                               totlen += len[i] + 1;
-                       }
+                       strncpy(ps + totlen, txt[i], len[i]);
+                       ps[totlen + len[i]] = '\n';
+                       ps[totlen + len[i] + 1] = 0;
+                       totlen += len[i] + 1;
                        GDKfree(txt[i]);
                }
        }
diff --git a/monetdb5/mal/mal_module.c b/monetdb5/mal/mal_module.c
--- a/monetdb5/mal/mal_module.c
+++ b/monetdb5/mal/mal_module.c
@@ -25,6 +25,8 @@ Module scopeJump[256][256];  /* to speed
 
 static void newSubScope(Module scope){
        scope->subscope = (Symbol *) GDKzalloc(MAXSCOPE * sizeof(Symbol));
+       if( scope->subscope  == NULL)
+               GDKerror("newSubScope:"MAL_MALLOC_FAIL);
 }
 
 void
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
@@ -1142,6 +1142,11 @@ fcnHeader(Client cntxt, int kind)
        }
        cntxt->backup = cntxt->curprg;
        cntxt->curprg = newFunction(putName("user"), fnme, kind);
+       if( cntxt->curprg == NULL){
+               parseError(cntxt, "Failed to create a newFunction()\n");
+               skipToEnd(cntxt);
+               return 0;
+       }
        curPrg = cntxt->curprg;
        curBlk = curPrg->def;
        curBlk->flowfixed = 0;
diff --git a/monetdb5/mal/mal_profiler.c b/monetdb5/mal/mal_profiler.c
--- a/monetdb5/mal/mal_profiler.c
+++ b/monetdb5/mal/mal_profiler.c
@@ -192,9 +192,9 @@ renderProfilerEvent(MalBlkPtr mb, MalStk
                stmt = instruction2str(mb, stk, pci, LIST_MAL_ALL);
                c = stmt;
 
-               while (c && *c && isspace((int)*c))
+               while (stmt && *c && isspace((int)*c))
                        c++;
-               if( *c){
+               if( stmt && *c){
                        stmtq = mal_quote(c, strlen(c));
                        if (stmtq != NULL) {
                                logadd("\"stmt\":\"%s\",%s", stmtq,prettify);
diff --git a/monetdb5/mal/mal_resolve.c b/monetdb5/mal/mal_resolve.c
--- a/monetdb5/mal/mal_resolve.c
+++ b/monetdb5/mal/mal_resolve.c
@@ -634,7 +634,7 @@ typeChecker(stream *out, Module scope, M
                                                                        
"'%s%s%s' undefined in: %s",
                                                                        
(getModuleId(p) ? getModuleId(p) : ""),
                                                                        
(getModuleId(p) ? "." : ""),
-                                                                       
getFunctionId(p), errsig);
+                                                                       
getFunctionId(p), errsig?errsig:"failed instruction2str()");
                                GDKfree(errsig);
                        } else
                                mb->errors = olderrors;
diff --git a/monetdb5/mal/mal_session.c b/monetdb5/mal/mal_session.c
--- a/monetdb5/mal/mal_session.c
+++ b/monetdb5/mal/mal_session.c
@@ -117,6 +117,10 @@ MSinitClientPrg(Client cntxt, str mod, s
                return;
        }
        cntxt->curprg = newFunction(putName("user"), putName(nme), 
FUNCTIONsymbol);
+       if( cntxt->curprg == 0){
+               GDKerror("MSinitClientPrg" "Failed to create function");
+               return;
+       }
        mb = cntxt->curprg->def;
        p = getSignature(cntxt->curprg);
        if (mod)
diff --git a/monetdb5/mal/mal_stack.h b/monetdb5/mal/mal_stack.h
--- a/monetdb5/mal/mal_stack.h
+++ b/monetdb5/mal/mal_stack.h
@@ -11,9 +11,6 @@
 #include "mal.h"
 
 #define stackSize(CNT) (sizeof(ValRecord)*(CNT) + offsetof(MalStack, stk))
-#define newStack(S,CNT) S= (MalStkPtr) GDKzalloc(stackSize(CNT));\
-               (S)->stksize=CNT;
-
 
 mal_export MalStkPtr newGlobalStack(int size);
 mal_export MalStkPtr reallocGlobalStack(MalStkPtr s, int cnt);
diff --git a/monetdb5/modules/atoms/blob.c b/monetdb5/modules/atoms/blob.c
--- a/monetdb5/modules/atoms/blob.c
+++ b/monetdb5/modules/atoms/blob.c
@@ -191,6 +191,8 @@ blob_tostr(str *tostr, int *l, blob *p)
                if (*tostr != NULL)
                        GDKfree(*tostr);
                *tostr = (str) GDKmalloc(expectedlen);
+               if( *tostr == NULL)
+                       return 0;
                *l = (int) expectedlen;
        }
        if (p->nitems == ~(size_t) 0) {
@@ -233,6 +235,8 @@ sqlblob_tostr(str *tostr, int *l, const 
                if (*tostr != NULL)
                        GDKfree(*tostr);
                *tostr = (str) GDKmalloc(expectedlen);
+               if( *tostr == NULL)
+                       return 0;
                *l = (int) expectedlen;
        }
        if (p->nitems == ~(size_t) 0) {
@@ -299,6 +303,9 @@ blob_fromstr(char *instr, int *l, blob *
                *val = (blob *) GDKmalloc(nbytes);
                *l = (int) nbytes;
        }
+       if( *val == NULL)
+               return 0;
+       
        result = *val;
        result->nitems = nitems;
 
@@ -383,6 +390,8 @@ sqlblob_fromstr(char *instr, int *l, blo
                *val = (blob *) GDKmalloc(nbytes);
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to