Browse code

simplify loop logic by using early continue

Hanno Böck authored on07/12/2019 18:43:52
Showing1 changed files
... ...
@@ -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,66 +178,51 @@ 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))
189
+                    findversion = item['variable'].search(filestr)
190
+                    if not findversion:
191
+                        continue
192
+                    findversion=findversion.group(1)
202 193
 
203
-                    if findversion and ematch:
194
+                    # Very ugly phpbb workaround
195
+                    if item['add_minor']:
196
+                        findversion = findversion.split('.')
197
+                        findversion[-1] = str(int(findversion[-1])
198
+                                              + int(item['add_minor']))
204 199
                         findversion = '.'.join(findversion)
205 200
 
206
-                        # Very ugly phpbb workaround
207
-                        if item['add_minor']:
208
-                            findversion = findversion.split('.')
209
-                            findversion[-1] = str(int(findversion[-1])
210
-                                                  + int(item['add_minor']))
211
-                            findversion = '.'.join(findversion)
212
-
213
-                        if not (versioncompare(item['safe'].split('.'),
214
-                                findversion.split('.'))) or \
215
-                                item['old_safe'].count(findversion) > 0:
216
-                            if opts.ALL:
217
-                                if opts.DEBUG:
218
-                                    print("File " + mfile)
219
-                                vulnprint(item['name'], findversion,
220
-                                          "ok", "", mfile, item['subdir'],
221
-                                          opts.OUTPUT)
222
-                        else:
201
+                    if not (versioncompare(item['safe'].split('.'),
202
+                            findversion.split('.'))) or \
203
+                            item['old_safe'].count(findversion) > 0:
204
+                        if opts.ALL:
223 205
                             if opts.DEBUG:
224 206
                                 print("File " + mfile)
225
-                            safev = "9999"
226
-                            for ver in item['old_safe']:
227
-                                if(versioncompare(ver.split('.'),
228
-                                   findversion.split('.'))
229
-                                   and not versioncompare(ver.split('.'),
230
-                                   safev.split('.'))):
231
-                                    safev = ver
232
-                            if safev == "9999":
233
-                                safev = item['safe']
234
-
235 207
                             vulnprint(item['name'], findversion,
236
-                                      safev, item['vuln'],
237
-                                      mfile, item['subdir'], opts.OUTPUT)
238
-
208
+                                      "ok", "", mfile, item['subdir'],
209
+                                      opts.OUTPUT)
239 210
                     else:
240 211
                         if opts.DEBUG:
241
-                            print("regexp failed for "
242
-                                  + item['name'] + " on " + mfile)
212
+                            print("File " + mfile)
213
+                        safev = "9999"
214
+                        for ver in item['old_safe']:
215
+                            if(versioncompare(ver.split('.'),
216
+                               findversion.split('.'))
217
+                               and not versioncompare(ver.split('.'),
218
+                               safev.split('.'))):
219
+                                safev = ver
220
+                        if safev == "9999":
221
+                            safev = item['safe']
222
+
223
+                        vulnprint(item['name'], findversion,
224
+                                  safev, item['vuln'],
225
+                                  mfile, item['subdir'], opts.OUTPUT)
243 226
 
244 227
 if opts.OUTPUT == 'xml':
245 228
     print('</freewvs>')