Explorar o código

confserver: Send an error response if JSON request is malformatted

Angus Gratton %!s(int64=6) %!d(string=hai) anos
pai
achega
2f2f0fbcbd
Modificáronse 2 ficheiros con 25 adicións e 1 borrados
  1. 7 1
      tools/kconfig_new/confserver.py
  2. 18 0
      tools/kconfig_new/test/test_confserver.py

+ 7 - 1
tools/kconfig_new/confserver.py

@@ -79,7 +79,13 @@ def run_server(kconfig, sdkconfig, default_version=MAX_PROTOCOL_VERSION):
         line = sys.stdin.readline()
         if not line:
             break
-        req = json.loads(line)
+        try:
+            req = json.loads(line)
+        except ValueError as e:  # json module throws JSONDecodeError (sublcass of ValueError) on Py3 but ValueError on Py2
+            response = {"version": default_version, "error": ["JSON formatting error: %s" % e]}
+            json.dump(response, sys.stdout)
+            print("\n")
+            continue
         before = confgen.get_json_values(config)
         before_ranges = get_ranges(config)
         before_visible = get_visible(config)

+ 18 - 0
tools/kconfig_new/test/test_confserver.py

@@ -64,6 +64,8 @@ def main():
 
         test_load_save(p, temp_sdkconfig_path)
 
+        test_invalid_json(p)
+
         print("Done. All passed.")
 
     finally:
@@ -143,5 +145,21 @@ def test_load_save(p, temp_sdkconfig_path):
     assert len(load_result["ranges"]) > 0
 
 
+def test_invalid_json(p):
+    print("Testing invalid JSON formatting...")
+
+    bad_escaping = r'{ "version" : 2, "load" : "c:\some\path\not\escaped\as\json" }'
+    p.send("%s\n" % bad_escaping)
+    readback = expect_json(p)
+    print(readback)
+    assert "json" in readback["error"][0].lower()
+
+    not_really_json = 'Hello world!!'
+    p.send("%s\n" % not_really_json)
+    readback = expect_json(p)
+    print(readback)
+    assert "json" in readback["error"][0].lower()
+
+
 if __name__ == "__main__":
     main()