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 c_sourcecode/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ int main_test(int argc, char *argv[])
for (int i = 0; i < dump_count; i++)
{
CRASHUPLOAD_INFO("List of dump file=%s\n", (dumps + i)->path);
process_file_entry((dumps + i)->path, argv[2], &config);
process_file_entry((dumps + i)->path, sizeof((dumps + i)->path), argv[2], &config);
CRASHUPLOAD_INFO("List of dump file After process_file_entry=%s\n", (dumps + i)->path);
len = strlen((dumps + i)->path);
if (len > 4 && strcmp((dumps + i)->path + len - 4, ".tgz") == 0)
Expand Down
4 changes: 2 additions & 2 deletions c_sourcecode/src/ratelimit/ratelimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ int is_upload_limit_reached(const char *file)
}

/* Validate numeric content of FIRST line */
for (size_t i = 0; first_line_data[i] != '\0' && first_line_data[i] != '\n' && i < sizeof(first_line_data); i++)
for (size_t i = 0; i < sizeof(first_line_data) && first_line_data[i] != '\0' && first_line_data[i] != '\n'; i++)
{
if (!isdigit((unsigned char)first_line_data[i]))
{
Expand Down Expand Up @@ -197,7 +197,7 @@ int is_recovery_time_reached(const char *deny_file)
fclose(fp);

/* Validate numeric content with bounds check */
for (size_t i = 0; buf[i] != '\0' && buf[i] != '\n' && i < sizeof(buf); i++)
for (size_t i = 0; i < sizeof(buf) && buf[i] != '\0' && buf[i] != '\n'; i++)
{
if (!isdigit((unsigned char)buf[i]))
return ALLOW_UPLOAD;
Expand Down
20 changes: 13 additions & 7 deletions c_sourcecode/src/scanner/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,8 @@ int processCrashTelemetryInfo(const char *rawfile, const char *log_path, bool t2
snprintf(Appname, sizeof(Appname), "%s", us + 1);

size_t pnamelen = (size_t)(us - containerName);
if (pnamelen >= sizeof(ProcessName))
pnamelen = sizeof(ProcessName) - 1;
if (pnamelen >= sizeof(containerName))
pnamelen = sizeof(containerName) - 1;
memcpy(ProcessName, containerName, pnamelen);
ProcessName[pnamelen] = '\0';
}
Expand Down Expand Up @@ -661,11 +661,11 @@ int processCrashTelemetryInfo(const char *rawfile, const char *log_path, bool t2
*
* Returns 0 on success, non-zero on error.
*/
int process_file_entry(char *fullpath, char *dump_type, const config_t *config)
int process_file_entry(char *fullpath, size_t fullpath_cap, char *dump_type, const config_t *config)
{
int sanitize_ret = -1;
char sanitized[256] = {0};
if (!fullpath || !dump_type || !config)
if (!fullpath || fullpath_cap == 0 || !dump_type || !config)
{
CRASHUPLOAD_INFO("process_file_entry():Invalid Parameter\n");
return -1;
Expand All @@ -683,7 +683,8 @@ int process_file_entry(char *fullpath, char *dump_type, const config_t *config)
char basename[PATH_MAX_LEN];
char fullcopy[PATH_MAX_LEN];

if (strlen(fullpath) >= PATH_MAX_LEN)
size_t fullpath_len = strnlen(fullpath, fullpath_cap);
if (fullpath_len >= fullpath_cap || fullpath_len >= PATH_MAX_LEN)
{
CRASHUPLOAD_INFO("process_file_entry(): File name is cross max length:%d\n", PATH_MAX_LEN);
return -1;
Expand Down Expand Up @@ -734,6 +735,11 @@ int process_file_entry(char *fullpath, char *dump_type, const config_t *config)
CRASHUPLOAD_INFO("Path too long after sanitization; skipping rename\n");
return -1;
}
if (strlen(newfull) >= fullpath_cap)
{
CRASHUPLOAD_INFO("Renamed path does not fit in caller buffer; skipping rename\n");
return -1;
}
/* Use rename() for atomic move; if target exists, choose to overwrite */
if (rename(fullpath, newfull) != 0)
{
Expand All @@ -749,7 +755,7 @@ int process_file_entry(char *fullpath, char *dump_type, const config_t *config)
else
{
CRASHUPLOAD_INFO("Renamed After retry %s -> %s\n", fullpath, newfull);
strcpy(fullpath, newfull);
snprintf(fullpath, fullpath_cap, "%s", newfull);
CRASHUPLOAD_INFO("After rename fullpath buffer updated=%s=\n", fullpath);
}
}
Expand All @@ -762,7 +768,7 @@ int process_file_entry(char *fullpath, char *dump_type, const config_t *config)
else
{
CRASHUPLOAD_INFO("Renamed %s -> %s\n", fullpath, newfull);
strcpy(fullpath, newfull);
snprintf(fullpath, fullpath_cap, "%s", newfull);
CRASHUPLOAD_INFO("After rename fullpath buffer updated=%s=\n", fullpath);
}
/* set fullpath to newfull for subsequent processing */
Expand Down
3 changes: 2 additions & 1 deletion c_sourcecode/src/scanner/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ int scanner_find_dumps(const char *path, dump_file_t **dumps, int *count, const
/**
* @brief Sanitise and rename a single dump file entry in-place.
* @param fullpath Path to the dump file; may be updated on rename.
* @param fullpath_cap Capacity of @p fullpath in bytes, including the terminator.
* @param dump_type String dump-type label ("coredump" or "minidump").
* @param config Application config.
* @return 0 on success.
*/
int process_file_entry(char *fullpath, char *dump_type, const config_t *config);
int process_file_entry(char *fullpath, size_t fullpath_cap, char *dump_type, const config_t *config);

/**
* @brief Extract the process name component from a dump file path.
Expand Down
4 changes: 2 additions & 2 deletions unittest/mainapp_gmock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ int scanner_find_dumps(const char *path, dump_file_t **dumps, int *count) {
/**
* Mock: Process file entry
*/
int process_file_entry(char *fullpath, char *dump_type, const config_t *config) {
if (!fullpath || !dump_type || !config) {
int process_file_entry(char *fullpath, size_t fullpath_cap, char *dump_type, const config_t *config) {
if (!fullpath || fullpath_cap == 0 || !dump_type || !config) {
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion unittest/scanner_gmock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ void scanner_cleanup(void)
* char dump_type[] = "0";
* config_t config;
*
* int result = process_file_entry(fullpath, dump_type, &config);
* int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &config);
*
* // Should return early since it's not a regular file
* EXPECT_EQ(result, 0);
Expand Down
30 changes: 15 additions & 15 deletions unittest/scanner_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int scanner_find_dumps(const char *path, dump_file_t **dumps, int *count, const
int scanner_get_sorted_dumps(dump_file_t **dumps, int *count);
void scanner_cleanup(void);
void scanner_update_test_state(dump_file_t *dumps, int count);
int process_file_entry(char *fullpath, char *dump_type, const config_t *config);
int process_file_entry(char *fullpath, size_t fullpath_cap, char *dump_type, const config_t *config);
int sanitize_filename_preserve_container(const char *fname, char *out, size_t outsz);
char *extract_pname(const char *filepath);
char *extract_appname(const char *filepath);
Expand Down Expand Up @@ -868,27 +868,27 @@ TEST_F(ScannerTest, ProcessTelemetry_LogMapperWithNoEqualsLine_Handled) {
TEST_F(ScannerTest, ProcessFileEntry_NullFullpath) {
char dump_type[] = "0";

int result = process_file_entry(nullptr, dump_type, &test_config);
int result = process_file_entry(nullptr, 0, dump_type, &test_config);
EXPECT_EQ(result, -1);
}

TEST_F(ScannerTest, ProcessFileEntry_NullDumpType) {
char fullpath[] = "/tmp/test.dmp";

int result = process_file_entry(fullpath, nullptr, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), nullptr, &test_config);
EXPECT_EQ(result, -1);
}

TEST_F(ScannerTest, ProcessFileEntry_NullConfig) {
char fullpath[] = "/tmp/test.dmp";
char dump_type[] = "0";

int result = process_file_entry(fullpath, dump_type, nullptr);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, nullptr);
EXPECT_EQ(result, -1);
}

TEST_F(ScannerTest, ProcessFileEntry_AllNullParameters) {
int result = process_file_entry(nullptr, nullptr, nullptr);
int result = process_file_entry(nullptr, 0, nullptr, nullptr);
EXPECT_EQ(result, -1);
}

Expand All @@ -900,7 +900,7 @@ TEST_F(ScannerTest, ProcessFileEntry_NotRegularFile) {
// Directory, not a file
set_mock_is_regular_file_behavior(0);

int result = process_file_entry(fullpath, dump_type, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &test_config);
EXPECT_EQ(result, 0); // Returns 0 but doesn't process
}

Expand All @@ -913,7 +913,7 @@ TEST_F(ScannerTest, ProcessFileEntry_ValidFile) {

set_mock_is_regular_file_behavior(1);

int result = process_file_entry(fullpath, dump_type, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &test_config);
// Success or error depending on file operations
EXPECT_GE(result, -1);
}
Expand All @@ -928,7 +928,7 @@ TEST_F(ScannerTest, ProcessFileEntry_FileWithInvalidChars) {

set_mock_is_regular_file_behavior(1);

int result = process_file_entry(fullpath, dump_type, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &test_config);
// Should attempt to rename or delete
EXPECT_GE(result, -1);
}
Expand All @@ -942,7 +942,7 @@ TEST_F(ScannerTest, ProcessFileEntry_DumpTypeNonZero) {

set_mock_is_regular_file_behavior(1);

int result = process_file_entry(fullpath, dump_type, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &test_config);
// Should process but not call telemetry
EXPECT_GE(result, -1);
}
Expand All @@ -956,7 +956,7 @@ TEST_F(ScannerTest, ProcessFileEntry_ContainerDelimiterFile) {

set_mock_is_regular_file_behavior(1);

int result = process_file_entry(fullpath, dump_type, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &test_config);
EXPECT_GE(result, -1);
}

Expand All @@ -970,7 +970,7 @@ TEST_F(ScannerTest, ProcessFileEntry_PathTooLong) {
snprintf(fullpath1, sizeof(fullpath1), "%s/%s", test_dump_dir,fullpath);

set_mock_is_regular_file_behavior(1);
int result = process_file_entry(fullpath1, dump_type, &test_config);
int result = process_file_entry(fullpath1, sizeof(fullpath1), dump_type, &test_config);
EXPECT_EQ(result, -1);
}

Expand All @@ -984,7 +984,7 @@ TEST_F(ScannerTest, ProcessFileEntry_EmptySanitizedName) {

set_mock_is_regular_file_behavior(1);

int result = process_file_entry(fullpath, dump_type, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &test_config);
// Should attempt to delete the file
EXPECT_GE(result, -1);
}
Expand All @@ -998,7 +998,7 @@ TEST_F(ScannerTest, ProcessFileEntry_RenameScenario) {

set_mock_is_regular_file_behavior(1);

int result = process_file_entry(fullpath, dump_type, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &test_config);
// Should attempt to rename
EXPECT_GE(result, -1);
}
Expand Down Expand Up @@ -1658,7 +1658,7 @@ TEST_F(ScannerTest, ProcessFileEntry_NoSlashInPath_DirnameDot) {
char fullpath[] = "nodirname_test.dmp";
char dump_type[] = "1";
set_mock_is_regular_file_behavior(1);
int result = process_file_entry(fullpath, dump_type, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &test_config);
EXPECT_GE(result, -1);
}

Expand All @@ -1674,7 +1674,7 @@ TEST_F(ScannerTest, ProcessFileEntry_RenameNeeded_DumpTypeNonZero) {
char dump_type[] = "1";
snprintf(fullpath, sizeof(fullpath), "%s/bad*rename.dmp", test_dump_dir);
set_mock_is_regular_file_behavior(1);
int result = process_file_entry(fullpath, dump_type, &test_config);
int result = process_file_entry(fullpath, sizeof(fullpath), dump_type, &test_config);
EXPECT_GE(result, -1);
}

Expand Down
Loading