Skip to content

Commit

Permalink
Address multiple line comments case
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Oct 4, 2024
1 parent 3027acf commit 3374a39
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ 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.
* @param num_elements The number of elements to allocate.
* @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

/**
Expand Down
22 changes: 15 additions & 7 deletions tests/test_code_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 3374a39

Please sign in to comment.