From 3374a39e8ab2f676f8c2c28731e613272b13cb6e Mon Sep 17 00:00:00 2001 From: Songling Han Date: Fri, 4 Oct 2024 22:39:33 +0000 Subject: [PATCH] Address multiple line comments case Signed-off-by: Songling Han --- src/common/common.h | 6 +++--- tests/test_code_conventions.py | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/common/common.h b/src/common/common.h index 887698f81..b15e244a3 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -56,7 +56,7 @@ extern "C" { * @param size The size of the memory to be allocated in bytes. * @return A pointer to the allocated memory. */ -#define OQS_MEM_malloc(size) malloc(size) +#define OQS_MEM_malloc(size) malloc(size) // IGNORE memory-check /** * Allocates memory for an array of elements of a given size. @@ -64,13 +64,13 @@ extern "C" { * @param element_size The size of each element in bytes. * @return A pointer to the allocated memory. */ -#define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size) +#define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size) // IGNORE memory-check /** * Duplicates a string. * @param str The string to be duplicated. * @return A pointer to the newly allocated string. */ -#define OQS_MEM_strdup(str) strdup(str) +#define OQS_MEM_strdup(str) strdup(str) // IGNORE memory-check #endif /** diff --git a/tests/test_code_conventions.py b/tests/test_code_conventions.py index 081bf8dd9..ed59fe14c 100644 --- a/tests/test_code_conventions.py +++ b/tests/test_code_conventions.py @@ -49,23 +49,31 @@ def test_spdx(): assert False def test_memory_functions(): - c_files = [] + c_h_files = [] for path, _, files in os.walk('src'): - c_files += [os.path.join(path, f) for f in files if f.endswith('.c')] + c_h_files += [os.path.join(path, f) for f in files if f.endswith(('.c', '.h'))] memory_functions = ['free', 'malloc', 'calloc', 'realloc', 'strdup'] okay = True - for fn in c_files: + for fn in c_h_files: with open(fn) as f: content = f.read() lines = content.splitlines() + in_multiline_comment = False for no, line in enumerate(lines, 1): - # Skip comments - if line.strip().startswith('//') or line.strip().startswith('/*'): + # Skip single-line comments + if line.strip().startswith('//'): continue - # Check if we're inside a multi-line comment - if '/*' in content[:content.find(line)] and '*/' not in content[:content.find(line)]: + # Check for start of multi-line comment + if '/*' in line and not in_multiline_comment: + in_multiline_comment = True + # Check for end of multi-line comment + if '*/' in line and in_multiline_comment: + in_multiline_comment = False + continue + # Skip lines inside multi-line comments + if in_multiline_comment: continue for func in memory_functions: if re.search(r'\b{}\('.format(func), line) and not re.search(r'\b_{}\('.format(func), line):