From 5a3d297d82a57aaf48c56fe64dc4a765e4850155 Mon Sep 17 00:00:00 2001 From: Dmitry Vodopyanov Date: Sat, 25 Nov 2023 19:01:17 +0100 Subject: [PATCH] handle empty ip netns output This patch fixes the crash which happens during execution of `wg-netns list`. The crash happens if nothing else from `wg-netns` is executed on machine yet, i.e., after the reboot. In that case, `ip -json netns` returns an empty string, not `[]`, so we get an exception if we pass empty line into `json.loads()`. ``` $ export WG_VERBOSE=1 $ /home/user/.local/bin/wg-netns list > ip -json netns error: Expecting value: line 1 column 1 (char 0) (JSONDecodeError) Traceback (most recent call last): File "/home/user/.local/bin/wg-netns", line 391, in main() File "/home/user/.local/bin/wg-netns", line 27, in main cli(sys.argv[1:]) File "/home/user/.local/bin/wg-netns", line 93, in cli data = json.loads(output) File "/usr/lib/python3.9/json/__init__.py", line 346, in loads return _default_decoder.decode(s) File "/usr/lib/python3.9/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.9/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ``` --- pyproject.toml | 2 +- wgnetns/main.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index cf7053f..131dc54 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "wgnetns" -version = "2.3.0" +version = "2.3.1" description = "wg-quick for network namespaces" authors = ["dadevel "] license = "MIT" diff --git a/wgnetns/main.py b/wgnetns/main.py index 74f34a4..e847112 100755 --- a/wgnetns/main.py +++ b/wgnetns/main.py @@ -90,6 +90,8 @@ def cli(args): namespace.teardown(check=not opts.force) elif opts.action == 'list': output = ip('-json', 'netns', capture=True) + if not output: + return data = json.loads(output) print('\n'.join(item['name'] for item in data)) elif opts.action == 'switch':