fixed statement type for logical expressions without operator + prepared OOM handling

Fri, 29 May 2015 12:29:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 29 May 2015 12:29:01 +0200
changeset 121
eea36bf5ffe2
parent 120
246c50447ebf
child 122
9a016d5fa9e7

fixed statement type for logical expressions without operator + prepared OOM handling

libidav/davqlparser.c file | annotate | diff | comparison | revisions
libidav/davqlparser.h file | annotate | diff | comparison | revisions
--- a/libidav/davqlparser.c	Fri May 29 10:15:12 2015 +0200
+++ b/libidav/davqlparser.c	Fri May 29 12:29:01 2015 +0200
@@ -374,7 +374,7 @@
 
 #define _error_context "(%.*s[->]%.*s%.*s)"
 #define _error_invalid "invalid statement"
-#define _error_unhandled "unhandled error " _error_context
+#define _error_out_of_memory "out of memory"
 #define _error_unexpected_token "unexpected token " _error_context
 #define _error_invalid_token "invalid token " _error_context
 #define _error_missing_path "expected path " _error_context
@@ -1210,6 +1210,7 @@
     token = ucx_list_get(token, consumed);
 
     if (token_is(token, DAVQL_TOKEN_OPERATOR)) {
+        expr->type = DAVQL_LOGICAL;
 
         davqloperator_t op = DAVQL_NOOP;
         if (tokenvalue_is(token, "and")) {
@@ -1252,9 +1253,7 @@
     }
     
     // set type and recover source text
-    if (total_consumed > 0) {
-        expr->type = DAVQL_LOGICAL;
-        
+    if (total_consumed > 0) {        
         expr->srctext.ptr = token_sstr(firsttoken).ptr;
         sstr_t lasttok = token_sstr(ucx_list_get(firsttoken, total_consumed-1));
         expr->srctext.length = lasttok.ptr-expr->srctext.ptr+lasttok.length;
@@ -1496,6 +1495,17 @@
 DavQLStatement* dav_parse_statement(sstr_t srctext) {
     DavQLStatement *stmt = calloc(1, sizeof(DavQLStatement));
     
+    // if we can't even get enough memory for the statement object or an error
+    // message, we can simply die without returning anything
+    if (!stmt) {
+        return NULL;
+    }
+    char *oommsg = strdup(_error_out_of_memory);
+    if (!oommsg) {
+        free(stmt);
+        return NULL;
+    }
+    
     // default values
     stmt->type = -1;
     stmt->depth = 1;
@@ -1530,6 +1540,13 @@
         stmt->errormessage = strdup(_error_invalid);
     }
     
+    if (stmt->errorcode = DAVQL_ERROR_OUT_OF_MEMORY) {
+        stmt->type = DAVQL_ERROR;
+        stmt->errormessage = oommsg;
+    } else {
+        free(oommsg);
+    }
+    
     return stmt;
 }
 
--- a/libidav/davqlparser.h	Fri May 29 10:15:12 2015 +0200
+++ b/libidav/davqlparser.h	Fri May 29 12:29:01 2015 +0200
@@ -323,8 +323,8 @@
 /** Nothing about the statement seems legit. */
 #define DAVQL_ERROR_INVALID -1
 
-/** Unhandled error */
-#define DAVQL_ERROR_UNHANDLED -2
+/** A call to malloc or calloc failed. */
+#define DAVQL_ERROR_OUT_OF_MEMORY -2
 
 /**
  * Starts an interactive debugger for a DavQLStatement.

mercurial