From 4efec581994339d330710315c08141efd4ccc7ef Mon Sep 17 00:00:00 2001 From: KuraZuzu Date: Wed, 30 Oct 2024 18:08:40 +0900 Subject: [PATCH] Fix undefined behavior by storing `std::string` objects instead of `const char*` pointers Previously, the test code for the 9-axis IMU was storing `const char*` pointers obtained from temporary `std::string` objects into a vector. Since these temporary `std::string` objects are destroyed immediately after the expression, the stored pointers became invalid (dangling pointers), leading to undefined behavior and causing tests to fail. This commit fixes the issue by changing the vector to store *std::string* objects directly instead of `const char*` pointers. By maintaining the `std::string` objects, we ensure that the data remains valid throughout its usage, and any `c_str()` calls on these strings return valid pointers. This change resolves the test failures and prevents potential crashes or incorrect data processing due to invalid memory access. --- test/test_driver.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/test/test_driver.cpp b/test/test_driver.cpp index f303765..8626519 100644 --- a/test/test_driver.cpp +++ b/test/test_driver.cpp @@ -198,29 +198,30 @@ unsigned int create_dummy_ascii_imu_data(unsigned char *buf, bool is_invalid) { unsigned int create_dummy_ascii_imu_data(unsigned char *buf, bool is_invalid, double *gyro, double *acc, double *mag, double temp) { rt_usb_9axisimu::Consts consts; - std::vector dummy_ascii_imu_data(consts.IMU_ASCII_DATA_SIZE); + std::vector dummy_ascii_imu_data(consts.IMU_ASCII_DATA_SIZE); if (is_invalid) { dummy_ascii_imu_data[consts.IMU_ASCII_TIMESTAMP] = "0.0"; } else { dummy_ascii_imu_data[consts.IMU_ASCII_TIMESTAMP] = "0"; } - dummy_ascii_imu_data[consts.IMU_ASCII_GYRO_X] = double_to_string(gyro[0]).c_str(); - dummy_ascii_imu_data[consts.IMU_ASCII_GYRO_Y] = double_to_string(gyro[1]).c_str(); - dummy_ascii_imu_data[consts.IMU_ASCII_GYRO_Z] = double_to_string(gyro[2]).c_str(); - dummy_ascii_imu_data[consts.IMU_ASCII_ACC_X] = double_to_string(acc[0]).c_str(); - dummy_ascii_imu_data[consts.IMU_ASCII_ACC_Y] = double_to_string(acc[1]).c_str(); - dummy_ascii_imu_data[consts.IMU_ASCII_ACC_Z] = double_to_string(acc[2]).c_str(); - dummy_ascii_imu_data[consts.IMU_ASCII_MAG_X] = double_to_string(mag[0]).c_str(); - dummy_ascii_imu_data[consts.IMU_ASCII_MAG_Y] = double_to_string(mag[1]).c_str(); - dummy_ascii_imu_data[consts.IMU_ASCII_MAG_Z] = double_to_string(mag[2]).c_str(); - dummy_ascii_imu_data[consts.IMU_ASCII_TEMP] = double_to_string(temp).c_str(); + dummy_ascii_imu_data[consts.IMU_ASCII_GYRO_X] = double_to_string(gyro[0]); + dummy_ascii_imu_data[consts.IMU_ASCII_GYRO_Y] = double_to_string(gyro[1]); + dummy_ascii_imu_data[consts.IMU_ASCII_GYRO_Z] = double_to_string(gyro[2]); + dummy_ascii_imu_data[consts.IMU_ASCII_ACC_X] = double_to_string(acc[0]); + dummy_ascii_imu_data[consts.IMU_ASCII_ACC_Y] = double_to_string(acc[1]); + dummy_ascii_imu_data[consts.IMU_ASCII_ACC_Z] = double_to_string(acc[2]); + dummy_ascii_imu_data[consts.IMU_ASCII_MAG_X] = double_to_string(mag[0]); + dummy_ascii_imu_data[consts.IMU_ASCII_MAG_Y] = double_to_string(mag[1]); + dummy_ascii_imu_data[consts.IMU_ASCII_MAG_Z] = double_to_string(mag[2]); + dummy_ascii_imu_data[consts.IMU_ASCII_TEMP] = double_to_string(temp); const char split_char = ','; const char newline_char = '\n'; buf[0] = (unsigned char)newline_char; unsigned int char_count = 1; for(int i = 0; i < consts.IMU_ASCII_DATA_SIZE; i++) { - for(int j = 0; j < (int)strlen(dummy_ascii_imu_data.at(i)); j++) { - buf[char_count] = (unsigned char)dummy_ascii_imu_data.at(i)[j]; + const char* data_str = dummy_ascii_imu_data.at(i).c_str(); + for(int j = 0; j < (int)strlen(data_str); j++) { + buf[char_count] = (unsigned char)data_str[j]; char_count++; } if(i != consts.IMU_ASCII_DATA_SIZE - 1) buf[char_count] = (unsigned char)split_char;