i trying run loop january1 2015 current date , fill value of each day in database. using julian day counter loop variable. no inside loop changing value of counter after looping 2 times value of counter changes abruptly garbage values. can reason?
for (julian_day_counter = 2457024; julian_day_counter < curr_day; \ julian_day_counter++) { (slot_counter = 1; slot_counter < 24; slot_counter++) { consumption = (rand() % (consumption_max_val - consumption_min_val + 1)) \ + consumption_min_val; /* form current slot value column string based on current slot value */ addr_port_strncpy((addr_u8bit_t *)slot_value_col, (addr_u8bit_t *)"slot_", \ addr_port_strlen((addr_c8bit_t *)"slot_")); addr_sprintf(temp_str, "%d", slot_counter); addr_port_strncat((addr_s8bit_t *)slot_value_col, (addr_c8bit_t *)temp_str, \ addr_short_str_len); /* comma separated column names */ addr_port_strncat((addr_s8bit_t *)col_name,(addr_c8bit_t *)slot_value_col, addr_short_str_len); addr_port_strncat((addr_s8bit_t *)col_name, \ (addr_c8bit_t *)" ,",addr_short_str_len); /* comma separated column values */ addr_sprintf(temp_str, "%f ,", consumption); addr_port_strncat((addr_s8bit_t *)col_value, (addr_c8bit_t *)temp_str, addr_short_str_len); } addr_port_strncat((addr_s8bit_t *)col_name, \ (addr_c8bit_t *)"julian_day",addr_short_str_len); addr_sprintf(temp_str, "%d", julian_day_counter); addr_port_strncat((addr_s8bit_t *)col_value, \ (addr_c8bit_t *)temp_str, addr_short_str_len); /* insert row consumption profile information in db*/ if( addr_failure == addr_db_local_insert(p_glb_pdb, addr_appliance_consumption_profile_table, (addr_c8bit_t *)col_name, (addr_c8bit_t *)col_value)) { ret_val = addr_failure; } }
below gdb snippet:
breakpoint 1, addr_db_update_dummy_appliance_consumption_profile ( p_glb_pdb=0x812a4b0, curr_day=2457151) @ addr_db_app.c:13184 13184 addr_s8bit_t temp_str[addr_short_str_len] = {'\0'}; (gdb) watch julian_day_counter hardware watchpoint 2: julian_day_counter (gdb) c continuing. hardware watchpoint 2: julian_day_counter old value = 0 new value = 2457024 0x08088d55 in addr_db_update_dummy_appliance_consumption_profile ( p_glb_pdb=0x812a4b0, curr_day=2457151) @ addr_db_app.c:13208 13208 (julian_day_counter = 2457024; julian_day_counter < curr_day; \ (gdb) c continuing. hardware watchpoint 2: julian_day_counter old value = 2457024 new value = 2457025 0x08088f2d in addr_db_update_dummy_appliance_consumption_profile ( p_glb_pdb=0x812a4b0, curr_day=2457151) @ addr_db_app.c:13208 13208 (julian_day_counter = 2457024; julian_day_counter < curr_day; \ (gdb) c continuing. hardware watchpoint 2: julian_day_counter old value = 2457025 new value = 2456880 0x00b6a33b in strncat () /lib/libc.so.6
well done @undur_gongor pointing out in comments question; culprit strncat()
. it's overwriting buffer , corrupting other variables. have buffer overflow.
you don't appear passing 3rd parameter correctly; this question:
#define buffer_size 64 char buff[buffer_size]; //use strncpy strncpy(buff, "string 1", buffer_size - 1); buff[buffer_size - 1] = '\0'; strncat(buff, "string 2", buffer_size - strlen(buff) - 1); strncat(buff, "string 3", buffer_size - strlen(buff) - 1);
Comments
Post a Comment