simplify loop logic by using early continue
Hanno Böck

Hanno Böck commited on 2019-12-07 18:43:52
Zeige 1 geänderte Dateien mit 12 Einfügungen und 29 Löschungen.

... ...
@@ -130,10 +130,8 @@ for sect in config.sections():
130 130
     item['subdir'] = int(config.get(sect, 'subdir'))
131 131
 
132 132
     # match magic
133
-    item['variable'] = []
134
-    for var in config.get(sect, 'variable').split(","):
135
-        item['variable'].append(re.compile(re.escape(var)
136
-                                + r"[^0-9\n\r]*[.]*([0-9.]*[0-9])[^0-9.]"))
133
+    item['variable'] = re.compile(re.escape(config.get(sect, 'variable'))
134
+                                + r"[^0-9\n\r]*[.]*([0-9.]*[0-9])[^0-9.]")
137 135
 
138 136
     # optional options
139 137
     if config.has_option(sect, 'extra_match'):
... ...
@@ -180,28 +178,18 @@ for fdir in args:
180 178
                     filestr = file.read()
181 179
                     file.close()
182 180
 
183
-                    if item['extra_match']:
184
-                        ematch = filestr.find(item['extra_match']) != -1
185
-                    elif item['extra_nomatch']:
186
-                        ematch = not filestr.find(item['extra_nomatch']) != -1
187
-                    else:
188
-                        ematch = True
189
-
190
-                    if (item['path_match']
191
-                       and not root.endswith(item['path_match'])):
181
+                    if ((item['extra_match']
182
+                       and item['extra_match'] not in filestr)
183
+                       or (item['extra_nomatch']
184
+                       and item['extra_nomatch'] not in filestr)
185
+                       or (item['path_match']
186
+                       and not root.endswith(item['path_match']))):
192 187
                         continue
193 188
 
194
-                    findversion = []
195
-                    for var in item['variable']:
196
-                        var = var.search(filestr)
197
-                        if not var:
198
-                            findversion = False
199
-                            break
200
-                        else:
201
-                            findversion.append(var.group(1))
202
-
203
-                    if findversion and ematch:
204
-                        findversion = '.'.join(findversion)
189
+                    findversion = item['variable'].search(filestr)
190
+                    if not findversion:
191
+                        continue
192
+                    findversion=findversion.group(1)
205 193
 
206 194
                     # Very ugly phpbb workaround
207 195
                     if item['add_minor']:
... ...
@@ -236,10 +224,5 @@ for fdir in args:
236 224
                                   safev, item['vuln'],
237 225
                                   mfile, item['subdir'], opts.OUTPUT)
238 226
 
239
-                    else:
240
-                        if opts.DEBUG:
241
-                            print("regexp failed for "
242
-                                  + item['name'] + " on " + mfile)
243
-
244 227
 if opts.OUTPUT == 'xml':
245 228
     print('</freewvs>')
246 229