From 569989e43cc29875f0e1aa3bd578e88649cc03e6 Mon Sep 17 00:00:00 2001 From: reginaldalfret Date: Fri, 16 Oct 2026 06:44:05 +0530 Subject: [PATCH] Fix #241, Keep array_size as string during validation in CHeaderParser Avoid casting array_size to int within the validation loop in CHeaderParser.py, which caused an AttributeError on isdigit() when the user provided an out-of-bounds input. Also add prompt_array_size helper function and unit tests. --- Subsystems/cmdGui/CHeaderParser.py | 50 ++++++++++-------- Subsystems/cmdGui/test_cheaderparser.py | 68 +++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 22 deletions(-) create mode 100644 Subsystems/cmdGui/test_cheaderparser.py diff --git a/Subsystems/cmdGui/CHeaderParser.py b/Subsystems/cmdGui/CHeaderParser.py index 439c701..160c1d1 100644 --- a/Subsystems/cmdGui/CHeaderParser.py +++ b/Subsystems/cmdGui/CHeaderParser.py @@ -109,6 +109,28 @@ def get_file_list(filename='CHeaderParser-hdr-paths.txt'): return [] +def prompt_array_size(array_name, initial_val=None, input_func=input): + """ + Validates array size or prompts the user until a valid integer + between 0 and 128 (inclusive) is provided. + """ + array_size = str(initial_val).strip() if initial_val is not None else "" + while not (array_size.isdigit() and int(array_size) in range(129)): + if not array_size.isdigit(): + print("Could not translate", array_name) + else: + print("Array size out of bounds. It must be between 0 and 128.") + try: + array_size = input_func( + (f"Please enter the defined value for " + f"{array_name} (0 - 128): ") + ).strip() + except (EOFError, KeyboardInterrupt): + break + return array_size + + + if __name__ == '__main__': # Get list of files to parse @@ -443,28 +465,12 @@ def get_file_list(filename='CHeaderParser-hdr-paths.txt'): print("Array size:", array_size) - # This while loop will make sure that - # the user input is both - # - a valid integer - # - between 0 and 128 (inclusively) - while not array_size.isdigit() or int( - array_size) not in range(129): - # User input is not an integer - if not array_size.isdigit(): - print("Could not translate", - array_name_size[1]) - else: - print( - "Array size out of bounds. It must be between 0 and 128." - ) - try: - # Try to translate user input to an integer - array_size = int( - input( - (f"Please enter the defined value for " - f"{array_name_size[1]} (0 - 128): "))) - except ValueError: - pass # Ignore non-integer and try again + # Ensure user input is a valid integer between 0 and 128 (inclusive) + array_size = prompt_array_size(array_name_size[1], + initial_val=array_size) + + # Update parameter length with validated array size + param_lens[-1] = array_size # Add string length argument to parameter list string_lens.append(array_size) diff --git a/Subsystems/cmdGui/test_cheaderparser.py b/Subsystems/cmdGui/test_cheaderparser.py new file mode 100644 index 0000000..882b03e --- /dev/null +++ b/Subsystems/cmdGui/test_cheaderparser.py @@ -0,0 +1,68 @@ +# +# NASA Docket No. GSC-19,200-1, and identified as "cFS Draco" +# +# Copyright (c) 2023 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import unittest +from unittest.mock import MagicMock + +from CHeaderParser import find_data_type_new, prompt_array_size + + +class TestCHeaderParser(unittest.TestCase): + + def test_find_data_type_new(self): + self.assertEqual(find_data_type_new('char', 'name[10]'), '--string') + self.assertEqual(find_data_type_new('boolean', 'flag'), '--uint8') + self.assertEqual(find_data_type_new('int32', 'count'), '--int32') + self.assertEqual(find_data_type_new('uint16', 'port'), '--uint16') + self.assertIsNone(find_data_type_new('custom_struct_t', 'data')) + + def test_prompt_array_size_already_valid_numeric_string(self): + mock_input = MagicMock() + result = prompt_array_size("my_array", initial_val="64", input_func=mock_input) + self.assertEqual(result, "64") + mock_input.assert_not_called() + + def test_prompt_array_size_boundary_values(self): + mock_input = MagicMock() + self.assertEqual(prompt_array_size("arr_zero", initial_val="0", input_func=mock_input), "0") + self.assertEqual(prompt_array_size("arr_max", initial_val="128", input_func=mock_input), "128") + mock_input.assert_not_called() + + def test_prompt_array_size_out_of_bounds_then_valid(self): + # Reproduces Issue #241: entering an integer out of range should not cause + # AttributeError: 'int' object has no attribute 'isdigit' on subsequent checks + inputs = iter(["200", "64"]) + result = prompt_array_size( + "OS_MAX_PATH_LEN", + initial_val="OS_MAX_PATH_LEN", + input_func=lambda _: next(inputs) + ) + self.assertEqual(result, "64") + + def test_prompt_array_size_non_numeric_then_valid(self): + inputs = iter(["not_a_number", "45"]) + result = prompt_array_size( + "OS_MAX_PATH_LEN", + initial_val="OS_MAX_PATH_LEN", + input_func=lambda _: next(inputs) + ) + self.assertEqual(result, "45") + + +if __name__ == '__main__': + unittest.main()