]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
Protect next() calls wit try/except inside generators
authorDimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
Sun, 26 Sep 2021 12:45:39 +0000 (14:45 +0200)
committerDimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
Sat, 26 Feb 2022 15:51:05 +0000 (16:51 +0100)
This fixes a DeepSource alert:

Unguarded next inside generator

Calls to `next()` should be inside `try-except` block.

When the iterator is exhausted, `StopIteration` exception is raised. When
used inside a generator, this can cause unexpected behavior. If not
handled, it will propagate out of the generator causing termination.
PEP-479 has been accepted to fix this problem. It will modify the
behavior of `StopIteration` in generators.

Each call to `next()` should be wrapped in a `try-except` block to explicitly
handle `StopIteration` exceptions.

Signed-off-by: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
 Veuillez saisir le message de validation pour vos modifications. Les lignes

test-f5-login.py
test-fortinet-login.py

index 21273d893204a87d48aa2e00ae01d881060d65e2..228bba4be703a7902da62de75551d01037d1b15b 100755 (executable)
@@ -72,8 +72,8 @@ else:
 
 # Build openconnect --cookie argument from the result:
 url = urlparse(res.url)
-if any(c.name == 'MRHSession' for c in s.cookies) and url.path.startswith('/vdesk/'):
-    cookie = next(c.value for c in s.cookies if c.name == 'MRHSession')
+cookie = next((c.value for c in s.cookies if c.name == 'MRHSession'), None)
+if cookie and url.path.startswith('/vdesk/'):
     if args.verbose:
         if cert:
             cert_and_key = ' \\\n        ' + ' '.join('%s "%s"' % (opt, quote(fn)) for opt, fn in zip(('-c', '-k'), cert) if fn)
index 91875234f69505581ea3d5f4ba49acd56d908703..1a6b2e6cc2c79ca725be41bf03ca0ae85007e0b6 100755 (executable)
@@ -60,8 +60,8 @@ res.raise_for_status()
 
 # Build openconnect --cookie argument from the result:
 url = urlparse(res.url)
-if any(c.name == 'SVPNCOOKIE' and c.value for c in s.cookies):
-    cookie = next(c.value for c in s.cookies if c.name == 'SVPNCOOKIE')
+cookie = next(c.value for c in s.cookies if c.name == 'SVPNCOOKIE' and c.value, None)
+if cookie:
     if args.verbose:
         if cert:
             cert_and_key = ' \\\n        ' + ' '.join('%s "%s"' % (opt, quote(fn)) for opt, fn in zip(('-c', '-k'), cert) if fn)