Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/val_common_framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ void val_handle_reboot_result(uint32_t test_progress);
void val_update_regression_report(uint32_t test_result, regre_report_t *regre_report);
void val_print_regression_report(regre_report_t *regre_report);

void val_mem_copy(char *dest, const char *src, size_t len);
void val_mem_copy(char *dest, size_t dest_size, const char *src, size_t len);

#endif /* VAL_COMMON_FRAMEWORK_H */
10 changes: 9 additions & 1 deletion inc/val_common_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ typedef enum {
* @param - ... : ellipses for variadic args
* @return - SUCCESS((Any positive number for character written)/FAILURE(0))
**/
uint32_t val_printf(print_verbosity_t verbosity, const char *msg, ...);
#if defined(__GNUC__) || defined(__clang__)
#define VAL_PRINTF_ATTR __attribute__((format(printf, 2, 3)))
#else
#define VAL_PRINTF_ATTR
#endif

uint32_t val_printf(print_verbosity_t verbosity, const char *msg, ...) VAL_PRINTF_ATTR;

#undef VAL_PRINTF_ATTR

#endif /* VAL_COMMON_LOG_H */
17 changes: 12 additions & 5 deletions src/val_common_framework.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,21 @@ void val_print_regression_report(regre_report_t *regre_report)
}

/**
* @brief - Copies 'len' bytes from source to destination buffer
* @param - dest : Destination buffer
* - src : Source buffer
* - len : Number of bytes to copy
* @brief - Copies 'len' bytes from source to destination buffer safely
* @param - dest : Destination buffer
* - dest_size : Size of the destination buffer
* - src : Source buffer
* - len : Number of bytes to copy
* @return - void
*/
void val_mem_copy(char *dest, const char *src, size_t len)
void val_mem_copy(char *dest, size_t dest_size, const char *src, size_t len)
{
if (dest == NULL || src == NULL || dest_size == 0 || len == 0)
return;

if (len > dest_size)
len = dest_size; /* Clamp to prevent writing past dest */
Comment on lines +183 to +184
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clamping logic should account for null termination. When len equals dest_size, the code will write up to the last byte of the destination buffer, leaving no room for a null terminator if one is expected. Consider clamping to dest_size - 1 if the destination is intended to be a null-terminated string, or document that this function does not null-terminate.

Copilot uses AI. Check for mistakes.

for (size_t i = 0; i < len; ++i)
dest[i] = src[i];
}
2 changes: 1 addition & 1 deletion src/val_common_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ uint32_t val_printf(print_verbosity_t verbosity, const char *msg, ...)

if (len > 0 && msg[len - 1] == '\n')
{
val_mem_copy(formatted_msg, msg, len - 1);
val_mem_copy(formatted_msg, sizeof(formatted_msg), msg, len - 1);
formatted_msg[len - 1] = '\r';
formatted_msg[len] = '\n';
formatted_msg[len + 1] = '\0';
Comment on lines +635 to 638
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the call to val_mem_copy, lines 636-638 write to indices len - 1, len, and len + 1 without checking if these indices are within the bounds of formatted_msg. If len is close to sizeof(formatted_msg), this could write beyond the buffer. The bounds check should be performed before these operations.

Copilot uses AI. Check for mistakes.
Expand Down