adds error message for invalid tag filter syntax

Wed, 30 May 2018 16:49:25 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 30 May 2018 16:49:25 +0200
changeset 392
4853fbad5014
parent 391
10305c5b0a16
child 393
438c8fe7d62f

adds error message for invalid tag filter syntax

dav/sync.c file | annotate | diff | comparison | revisions
dav/sync.h file | annotate | diff | comparison | revisions
--- a/dav/sync.c	Wed May 30 16:25:57 2018 +0200
+++ b/dav/sync.c	Wed May 30 16:49:25 2018 +0200
@@ -256,11 +256,12 @@
 }
 
 SyncTagFilter* parse_tagfilter_string(const char* filterstring) {
+    SyncTagFilter* tagfilter = malloc(sizeof(SyncTagFilter));
     if (!filterstring) {
-        return NULL;
+        memset(tagfilter, 0, sizeof(SyncTagFilter));
+        return tagfilter;
     }
     
-    SyncTagFilter* tagfilter = malloc(sizeof(SyncTagFilter));
     tagfilter->mode = DAV_SYNC_TAGFILTER_AND;
     tagfilter->subfilter_count = 0;
     tagfilter->tags = parse_csv_taglist(filterstring, strlen(filterstring));
@@ -339,7 +340,7 @@
 }
 
 static int res_matches_tags(DavResource *res, SyncTagFilter *tagfilter) {
-    if(!tagfilter) {
+    if(tagfilter->mode == DAV_SYNC_TAGFILTER_OFF) {
         return 1;
     }
     
@@ -414,6 +415,13 @@
         return -1;
     }
     
+    // if there are syntax errors in the command line, fail asap.
+    SyncTagFilter* tagfilter = parse_tagfilter_string(cmd_getoption(a, "tags"));
+    if (!tagfilter) {
+        fprintf(stderr, "Malformed tag filter\n");
+        return -1;
+    }
+    
     SyncDirectory *dir = scfg_get_dir(a->argv[0]);
     if(!dir) {
         fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]);
@@ -531,9 +539,8 @@
                 res = res->next;
                 continue;
             }
-            
-            if (!res_matches_tags(res,
-                    parse_tagfilter_string(cmd_getoption(a, "tags")))) {
+                        
+            if (!res_matches_tags(res, tagfilter)) {
                 if(!cmd_getoption(a, "remove")) {
                     localres_keep(db, res->path);
                 }
--- a/dav/sync.h	Wed May 30 16:25:57 2018 +0200
+++ b/dav/sync.h	Wed May 30 16:49:25 2018 +0200
@@ -118,6 +118,8 @@
  */
 typedef struct SyncTagFilter SyncTagFilter;
 
+/* OFF must be zero, other constants are arbitrary */
+#define DAV_SYNC_TAGFILTER_OFF     0
 #define DAV_SYNC_TAGFILTER_AND     1
 #define DAV_SYNC_TAGFILTER_OR      2
 #define DAV_SYNC_TAGFILTER_ONE     3

mercurial