Changeset: 3105e3b5e66e for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=3105e3b5e66e
Modified Files:
        monetdb5/mal/mal_interpreter.c
        monetdb5/modules/mal/pcre.c
Branch: default
Log Message:

Merge with Jul2017 branch.


diffs (truncated from 842 to 300 lines):

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
@@ -1137,8 +1137,7 @@ str runMALsequence(Client cntxt, MalBlkP
                                        for (j = 0; j < l->retc; j++)
                                                if (getArg(l, j) == 
exceptionVar)
                                                        break;
-                                               else if (getArgName(mb, l, j) ||
-                                                                
strcmp(getArgName(mb, l, j), "ANYexception") == 0)
+                                               else if (strcmp(getArgName(mb, 
l, j), "ANYexception") == 0)
                                                        break;
                                        if (j < l->retc)
                                                break;
diff --git a/monetdb5/modules/mal/pcre.c b/monetdb5/modules/mal/pcre.c
--- a/monetdb5/modules/mal/pcre.c
+++ b/monetdb5/modules/mal/pcre.c
@@ -140,7 +140,7 @@ re_match_ignore(const char *s, RE *patte
 {
        RE *r;
 
-       for(r = pattern; r; r = r->n) {
+       for (r = pattern; r; r = r->n) {
                if (!*s ||
                        (!r->search && strncasecmp(s, r->k, r->len) != 0) ||
                        (r->search && (s = strcasestr(s, r->k)) == NULL))
@@ -155,7 +155,7 @@ re_match_no_ignore(const char *s, RE *pa
 {
        RE *r;
 
-       for(r = pattern; r; r = r->n) {
+       for (r = pattern; r; r = r->n) {
                if (!*s ||
                        (!r->search && strncmp(s, r->k, r->len) != 0) ||
                        (r->search && (s = strstr(s, r->k)) == NULL))
@@ -165,6 +165,18 @@ re_match_no_ignore(const char *s, RE *pa
        return 1;
 }
 
+static void
+re_destroy(RE *p)
+{
+       while (p) {
+               RE *n = p->n;
+
+               GDKfree(p->k);
+               GDKfree(p);
+               p = n;
+       }
+}
+
 static RE *
 re_create(const char *pat, int nr)
 {
@@ -180,43 +192,36 @@ re_create(const char *pat, int nr)
        r->n = NULL;
        r->search = 0;
        r->skip = 0;
+       r->k = NULL;
 
        if (*p == '%') {
                p++; /* skip % */
                r->search = 1;
        }
        q = p;
-       while((q = strchr(p, '%')) != NULL) {
+       while ((q = strchr(p, '%')) != NULL) {
                *q = 0;
                n->k = GDKstrdup(p);
+               if (n->k == NULL)
+                       goto bailout;
                n->len = (int) strlen(n->k);
-               n->n = NULL;
-               if ((--nr) > 0) {
+               if (--nr > 0) {
                        n = n->n = (RE*)GDKmalloc(sizeof(RE));
-                       if ( n == NULL){
-                               GDKfree(x);
-                               GDKfree(r);
-                               return NULL;
-                       }
+                       if (n == NULL)
+                               goto bailout;
                        n->search = 1;
                        n->skip = 0;
+                       n->n = NULL;
+                       n->k = NULL;
                }
-               p = q+1;
+               p = q + 1;
        }
        GDKfree(x);
        return r;
-}
-
-static void
-re_destroy( RE *p)
-{
-       while(p) {
-               RE *n = p->n;
-
-               GDKfree(p->k);
-               GDKfree(p);
-               p = n;
-       }
+  bailout:
+       GDKfree(x);
+       re_destroy(r);
+       return NULL;
 }
 
 static str
@@ -231,9 +236,9 @@ pcre_compile_wrap(pcre **res, const char
                options |= PCRE_CASELESS;
 
        if ((r = pcre_compile(pattern, options, &err_p, &errpos, NULL)) == 
NULL) {
-               throw(MAL,"pcre.compile", OPERATION_FAILED
-                       " with\n'%s'\nat %d in\n'%s'.\n",
-                               err_p, errpos, pattern);
+               throw(MAL, "pcre.compile", OPERATION_FAILED
+                         " with\n'%s'\nat %d in\n'%s'.\n",
+                         err_p, errpos, pattern);
        }
        *res = r;
        return MAL_SUCCEED;
@@ -241,44 +246,44 @@ pcre_compile_wrap(pcre **res, const char
        (void) res;
        (void) pattern;
        (void) insensitive;
-       throw(MAL,"pcre.compile", "Database was compiled without PCRE 
support.");
+       throw(MAL, "pcre.compile", "Database was compiled without PCRE 
support.");
 #endif
 }
 
 /* these two defines are copies from gdk_select.c */
 
 /* scan select loop with candidates */
-#define candscanloop(TEST)                                                     
                        \
-       do {                                                                    
                                        \
-               ALGODEBUG fprintf(stderr,                                       
                        \
-                           "#BATselect(b=%s#"BUNFMT",s=%s,anti=%d): "  \
-                           "scanselect %s\n", BATgetId(b), BATcount(b),        
\
-                           s ? BATgetId(s) : "NULL", anti, #TEST);             
        \
-               while (p < q) {                                                 
                                \
-                       o = *candlist++;                                        
                                \
-                       r = (BUN) (o - off);                                    
                        \
-                       v = BUNtail(bi, r);                                     
                                \
-                       if (TEST)                                               
                                        \
-                               bunfastapp(bn, &o);                             
                                \
-                       p++;                                                    
                                        \
-               }                                                               
                                                \
+#define candscanloop(TEST)                                                     
                                        \
+       do {                                                                    
                                                        \
+               ALGODEBUG fprintf(stderr,                                       
                                        \
+                                                 
"#BATselect(b=%s#"BUNFMT",s=%s,anti=%d): "    \
+                                                 "scanselect %s\n", 
BATgetId(b), BATcount(b),  \
+                                                 s ? BATgetId(s) : "NULL", 
anti, #TEST);               \
+               while (p < q) {                                                 
                                                \
+                       o = *candlist++;                                        
                                                \
+                       r = (BUN) (o - off);                                    
                                        \
+                       v = BUNtail(bi, r);                                     
                                                \
+                       if (TEST)                                               
                                                        \
+                               bunfastapp(bn, &o);                             
                                                \
+                       p++;                                                    
                                                        \
+               }                                                               
                                                                \
        } while (0)
 
 /* scan select loop without candidates */
-#define scanloop(TEST)                                                         
                        \
-       do {                                                                    
                                        \
-               ALGODEBUG fprintf(stderr,                                       
                        \
-                           "#BATselect(b=%s#"BUNFMT",s=%s,anti=%d): "  \
-                           "scanselect %s\n", BATgetId(b), BATcount(b),        
\
-                           s ? BATgetId(s) : "NULL", anti, #TEST);             
        \
-               while (p < q) {                                                 
                                \
-                       v = BUNtail(bi, p-off);                                 
                        \
-                       if (TEST) {                                             
                                        \
-                               o = (oid) p;                                    
                                \
-                               bunfastapp(bn, &o);                             
                                \
-                       }                                                       
                                                \
-                       p++;                                                    
                                        \
-               }                                                               
                                                \
+#define scanloop(TEST)                                                         
                                        \
+       do {                                                                    
                                                        \
+               ALGODEBUG fprintf(stderr,                                       
                                        \
+                                                 
"#BATselect(b=%s#"BUNFMT",s=%s,anti=%d): "    \
+                                                 "scanselect %s\n", 
BATgetId(b), BATcount(b),  \
+                                                 s ? BATgetId(s) : "NULL", 
anti, #TEST);               \
+               while (p < q) {                                                 
                                                \
+                       v = BUNtail(bi, p-off);                                 
                                        \
+                       if (TEST) {                                             
                                                        \
+                               o = (oid) p;                                    
                                                \
+                               bunfastapp(bn, &o);                             
                                                \
+                       }                                                       
                                                                \
+                       p++;                                                    
                                                        \
+               }                                                               
                                                                \
        } while (0)
 
 static str
@@ -319,7 +324,6 @@ pcre_likeselect(BAT **bnp, BAT *b, BAT *
        pe = pcre_study(re, 0, &error);
        if (error != NULL) {
                pcre_free(re);
-               pcre_free_study(pe);
                throw(MAL, "pcre.likeselect",
                          OPERATION_FAILED ": studying pattern \"%s\" 
failed\n", pat);
        }
@@ -562,28 +566,31 @@ pcre_replace(str *res, const char *origi
        int len_replacement = (int) strlen(replacement);
        int capture_offsets[MAX_NR_CAPTURES * 2], ncaptures = 0, len_del = 0;
 
-       for (i = 0; i < (int)strlen(flags); i++) {
-               if (flags[i] == 'e') {
-                       exec_options -= PCRE_NOTEMPTY;
-/*
-                       mnstr_printf(GDKout, "exec_options %d, PCRE_NOTEMPTY 
%d\n",
-                                       exec_options, PCRE_NOTEMPTY);
-*/
-               } else if (flags[i] == 'i') {
+       while (*flags) {
+               switch (*flags) {
+               case 'e':
+                       exec_options &= ~PCRE_NOTEMPTY;
+                       break;
+               case 'i':
                        compile_options |= PCRE_CASELESS;
-               } else if (flags[i] == 'm') {
+                       break;
+               case 'm':
                        compile_options |= PCRE_MULTILINE;
-               } else if (flags[i] == 's') {
+                       break;
+               case 's':
                        compile_options |= PCRE_DOTALL;
-               } else if (flags[i] == 'x') {
+                       break;
+               case 'x':
                        compile_options |= PCRE_EXTENDED;
-               } else {
-                       throw(MAL,"pcre_replace",OPERATION_FAILED "unsupported 
flag character '%c'\n", flags[i]);
+                       break;
+               default:
+                       throw(MAL, "pcre_replace", ILLEGAL_ARGUMENT ": 
unsupported flag character '%c'\n", *flags);
                }
+               flags++;
        }
 
        if ((pcre_code = pcre_compile(pattern, compile_options, &err_p, 
&errpos, NULL)) == NULL) {
-               throw(MAL,"pcre_replace",OPERATION_FAILED "pcre compile of 
pattern (%s) failed at %d with\n'%s'.\n", pattern, errpos, err_p);
+               throw(MAL, "pcre_replace", OPERATION_FAILED ": pcre compile of 
pattern (%s) failed at %d with\n'%s'.\n", pattern, errpos, err_p);
        }
 
        /* Since the compiled pattern is going to be used several times, it is
@@ -591,18 +598,23 @@ pcre_replace(str *res, const char *origi
         * taken for matching.
         */
        extra = pcre_study(pcre_code, 0, &err_p);
+       if (err_p != NULL) {
+               pcre_free(pcre_code);
+               throw(MAL, "pcre_replace", OPERATION_FAILED ": pcre study of 
pattern (%s) failed with '%s'.\n", pattern, err_p);
+       }
        pcre_fullinfo(pcre_code, extra, PCRE_INFO_CAPTURECOUNT, &i);
        ovecsize = (i + 1) * 3;
        if ((ovector = (int *) GDKmalloc(sizeof(int) * ovecsize)) == NULL) {
                pcre_free(pcre_code);
+               pcre_free_study(extra);
                throw(MAL, "pcre_replace", SQLSTATE(HY001) MAL_MALLOC_FAIL);
        }
 
        i = 0;
        do {
                j = pcre_exec(pcre_code, extra, origin_str, len_origin_str,
-                                               offset, exec_options, ovector, 
ovecsize);
-               if (j > 0){
+                                         offset, exec_options, ovector, 
ovecsize);
+               if (j > 0) {
                        capture_offsets[i] = ovector[0];
                        capture_offsets[i+1] = ovector[1];
                        ncaptures++;
@@ -610,53 +622,48 @@ pcre_replace(str *res, const char *origi
                        len_del += (ovector[1] - ovector[0]);
                        offset = ovector[1];
                }
-       } while((j > 0) && (offset < len_origin_str) && (ncaptures < 
MAX_NR_CAPTURES));
+       } while (j > 0 && offset < len_origin_str && ncaptures < 
MAX_NR_CAPTURES);
        pcre_free_study(extra);
 
-       if (ncaptures > 0){
+       if (ncaptures > 0) {
                tmpres = GDKmalloc(len_origin_str - len_del + (len_replacement 
* ncaptures) + 1);
-               if (!tmpres) {
-                       pcre_free(pcre_code);
-                       GDKfree(ovector);
-                       throw(MAL, "pcre_replace", SQLSTATE(HY001) 
MAL_MALLOC_FAIL);
-               }
+               if (tmpres) {
+                       j = k = 0;
+
+                       /* possibly copy the substring before the first captured
+                        * substring */
+                       strncpy(tmpres, origin_str, capture_offsets[j]);
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to