From 2acab147ef9978ad1e9d2730c193c1852e817c9f Mon Sep 17 00:00:00 2001 From: Maarten Date: Wed, 26 Mar 2025 14:14:09 +0100 Subject: [PATCH] Added simple error flag to handle packet issues. --- .vscode/launch.json | 37 ++++++++++++++++++++++++++++ asterix/test/test_cat62error.py | 13 ++++++++++ asterix_decoder.egg-info/PKG-INFO | 17 ++++++++++--- asterix_decoder.egg-info/SOURCES.txt | 12 +++++---- src/asterix/InputParser.cpp | 1 + src/asterix/InputParser.h | 5 +++- src/python/python_parser.cpp | 5 ++++ 7 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 asterix/test/test_cat62error.py diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..7839086e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,37 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python C++ Debugger", + "type": "pythoncpp", + "request": "launch", + "pythonLaunchName": "Python: Current File", + "cppAttachName": "(gdb) Attach" + }, + { + "name": "(gdb) Attach", + "type": "cppdbg", + "request": "attach", + "program": "${workspaceFolder}/.venv/bin/python", + "processId": "", + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + }, + { + "name": "Python: Current File", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/asterix/test/test_cat62error.py b/asterix/test/test_cat62error.py new file mode 100644 index 00000000..f8eb83b5 --- /dev/null +++ b/asterix/test/test_cat62error.py @@ -0,0 +1,13 @@ + + +import unittest +import asterix + + +data=b" \xd4P\xb4\xe5\x00\x00#\x00'\xff\xff\x00!\x000\x06\x06\x84\x00\x00 \x0f\x01y\xa0\x11\x94\x11\x94\x03 \x03 '\x10\x11\x9b\x1f\x13\x8f\x02\x04\xd3_\xe8\x00\x00\x95\x8e\xc7\x00\x0c\x0e\xcb\xff\xff\x00\x00A\x011\xa0\x00\x00\x0e\x03\x16P\xd6\x9d\x00\x02%c\xb2\x03\x03\xb3\x02t\xf4\x80\x91\x00\xff\x0f\x00\x00!\x01\x00\x00\xa5n\x01\x01\x00\xd4\x87\xb6\xe5\x00\x00\x17\x00\x1a\x00\x00\x00\x16\x00 \x04\x04\x84\x00\x00 \x0f\x01y\xa0\x03\x84\x03\x84\x01,\x01,'\x10\x10\x9b\x1f\x13\x8f\x02\x04\xd3_\xe8\x00\x00\x99\xd0\x00\x0f\xc7\x96\x00\x1e\x00\x04a\x01q\xa0\x00\x00\r\x9do\xe0:P\x00\x00:\xbd\x00C&t\x16\x03\xf1\x9c\x02\xa6t\x80\x91\x00\xff\x07\x00\x00!\x01\x00\x00\xad\t_-;\\\xd4\xb5\\\xe5\x00\x00\x13\x00\x15\x00\x00\x00\x12\x00\x1b\x03\x03\x84\x00\x00 \x0f\x01y\xa0:f:f\x08\xfc\x08\xfc'\x10\x11\x9b\x0f\x13\x8f\x02\x04\xd3_\xe8\x00\x00\x93\xa2\x91\x00\x0f\xd5\xaf\x00\x00\x00\x00\xa9\x03\x80\xc1\x00\xff\x15\x00\x00!\x01\x00\x00\xa4f\xd1\r\xc9\x0c\x07\x14q\xd4\n\x02\xe5\x00\x00\x11\x00\x15\x00\x00\x00\x12\x00\x17\x01\x01\xc4\x04\x07\x01\x067\x86($\xffy\xe0\x00\x00\x00\x00\x00\x17\x06\x08 \x00\r\xd2,\x00\x85\x00\x00\x00\x00\x00\xfa\x00\xfa\x00\xc8\x00\xc8'\x10\x02\x8f " + + +class TestCat62Error(unittest.TestCase): + def test_cat62error(self): + with self.assertRaises(RuntimeError): + asterix.parse(data) \ No newline at end of file diff --git a/asterix_decoder.egg-info/PKG-INFO b/asterix_decoder.egg-info/PKG-INFO index e1da4d1c..1c829a75 100644 --- a/asterix_decoder.egg-info/PKG-INFO +++ b/asterix_decoder.egg-info/PKG-INFO @@ -1,6 +1,6 @@ -Metadata-Version: 2.1 -Name: asterix-decoder -Version: 0.7.4 +Metadata-Version: 2.4 +Name: asterix_decoder +Version: 0.7.9 Summary: ASTERIX decoder in Python Home-page: https://github.com/CroatiaControlLtd/asterix Download-URL: https://github.com/CroatiaControlLtd/asterix @@ -27,6 +27,17 @@ Classifier: Programming Language :: Python :: 3.9 Classifier: Programming Language :: Python :: 3.10 License-File: LICENSE License-File: COPYING +Dynamic: author +Dynamic: author-email +Dynamic: classifier +Dynamic: description +Dynamic: download-url +Dynamic: home-page +Dynamic: keywords +Dynamic: license +Dynamic: license-file +Dynamic: platform +Dynamic: summary asterix ======= diff --git a/asterix_decoder.egg-info/SOURCES.txt b/asterix_decoder.egg-info/SOURCES.txt index 1cdcb4fb..d3af2e3d 100644 --- a/asterix_decoder.egg-info/SOURCES.txt +++ b/asterix_decoder.egg-info/SOURCES.txt @@ -40,11 +40,11 @@ asterix/config/asterix_cat004_1_12.xml asterix/config/asterix_cat008_1_0.xml asterix/config/asterix_cat010_1_1.xml asterix/config/asterix_cat011_1_2.xml -asterix/config/asterix_cat019_1_2.xml -asterix/config/asterix_cat020_1_7.xml -asterix/config/asterix_cat021_2_4.xml -asterix/config/asterix_cat023_1_2.xml -asterix/config/asterix_cat025_1_2.xml +asterix/config/asterix_cat019_1_3.xml +asterix/config/asterix_cat020_1_10.xml +asterix/config/asterix_cat021_2_6.xml +asterix/config/asterix_cat023_1_3.xml +asterix/config/asterix_cat025_1_5.xml asterix/config/asterix_cat030_6_2.xml asterix/config/asterix_cat031_6_2.xml asterix/config/asterix_cat032_7_0.xml @@ -73,7 +73,9 @@ asterix/sample_data/cat_062_065.pcap asterix/test/__init__.py asterix/test/parsing_error_1.xml asterix/test/parsing_error_2.xml +asterix/test/test_cat62error.py asterix/test/test_init.py +asterix/test/test_memory_leak.py asterix/test/test_parse.py asterix/test/valgrind-python.supp.txt asterix_decoder.egg-info/PKG-INFO diff --git a/src/asterix/InputParser.cpp b/src/asterix/InputParser.cpp index 9c40b7e6..ed8f136d 100644 --- a/src/asterix/InputParser.cpp +++ b/src/asterix/InputParser.cpp @@ -71,6 +71,7 @@ InputParser::parsePacket(const unsigned char *m_pBuffer, unsigned int m_nBufferS // parse Asterix data if (dataLen <= 3 || dataLen > m_nDataLength) { Tracer::Error("Wrong Asterix data length (%d)", dataLen); + this->raisedError = true; if (dataLen <= 3) { // otherwise finish diff --git a/src/asterix/InputParser.h b/src/asterix/InputParser.h index bb619ff0..a6ba3362 100644 --- a/src/asterix/InputParser.h +++ b/src/asterix/InputParser.h @@ -48,10 +48,13 @@ class InputParser { bool filterOutItem(int cat, std::string item, const char *name); bool isFiltered(int cat, std::string item, const char *name); + bool raisedErrorFlag() { return raisedError; } + void clearErrorFlag() { raisedError = false; } + private: AsterixDefinition *m_pDefinition; // Asterix definitions - + bool raisedError = false; }; #endif /* INPUTPARSER_H_ */ diff --git a/src/python/python_parser.cpp b/src/python/python_parser.cpp index e38c5d2c..89e6fc30 100644 --- a/src/python/python_parser.cpp +++ b/src/python/python_parser.cpp @@ -87,6 +87,11 @@ PyObject *python_parse(const unsigned char *pBuf, Py_ssize_t len, int verbose) { if (inputParser) { AsterixData *pData = inputParser->parsePacket(pBuf, len, nTimestamp); + if (inputParser->raisedErrorFlag()){ + PyErr_SetString(PyExc_RuntimeError, "Error parsing packet"); + inputParser->clearErrorFlag(); + return NULL; + } if (pData) { // convert to Python format PyObject *lst = pData->getData(verbose); delete pData;