Browse code

fixed cleanup and error reporting in cli_loadyara()

Kevin Lin authored on 2015/02/13 06:57:40
Showing 1 changed files
... ...
@@ -3137,11 +3137,11 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
3137 3137
     }
3138 3138
 
3139 3139
     if (str_error > 0) {
3140
-        cli_warnmsg("load_oneyara: clamav cannot support %d input strings for %s, skipping\n", str_error, rule->id);
3140
+        cli_warnmsg("load_oneyara: clamav cannot support %d input strings, skipping\n", str_error);
3141 3141
         yara_malform++;
3142 3142
         ytable_delete(&ytable);
3143 3143
         (*sigs)--;
3144
-        return ret;
3144
+        return ret; /* kill determined by ret */
3145 3145
     } else if (ytable.tbl_cnt == 0) {
3146 3146
         cli_warnmsg("load_oneyara: yara contains no supported strings, skipping\n");
3147 3147
         yara_malform++;
... ...
@@ -3296,20 +3296,28 @@ static int cli_loadyara(FILE *fs, struct cl_engine *engine, unsigned int *signo,
3296 3296
         STAILQ_REMOVE(&compiler.rules, rule, _yc_rule, link);
3297 3297
 
3298 3298
         rules++;
3299
-        sigs++;
3299
+        sigs++; /* can be decremented by load_oneyara */
3300 3300
 
3301 3301
         /* TODO - PUA and engine->ignored */
3302 3302
         rc = load_oneyara(rule, engine, options, &sigs);
3303
+        if (rc != CL_SUCCESS) {
3304
+            cli_errmsg("cli_loadyara: problem parsing yara rule %s\n", rule->id);
3305
+            free_yararule(rule);
3306
+            break;
3307
+        }
3308
+
3303 3309
         free_yararule(rule);
3310
+    }
3304 3311
 
3305
-        if (rc != CL_SUCCESS)
3306
-            break;
3312
+    /* clean up rules queue on error */
3313
+    while (!STAILQ_EMPTY(&compiler.rules)) {
3314
+        rule = STAILQ_FIRST(&compiler.rules);
3315
+        STAILQ_REMOVE(&compiler.rules, rule, _yc_rule, link);
3316
+        free_yararule(rule);
3307 3317
     }
3308 3318
 
3309
-    if(rc) {
3310
-        cli_errmsg("cli_loadyara: problem parsing yara rule %s\n", rule->id);
3319
+    if(rc)
3311 3320
         return rc;
3312
-    }
3313 3321
 
3314 3322
     if(!rules) {
3315 3323
         cli_errmsg("cli_loadyara: empty database file\n");