i am trying to run a loop from january1 2015 to current date and fill value of each day in database. I am using a julian day counter as loop variable. No where inside the loop i am changing the value of counter but after looping 2 times the value of counter changes abruptly to garbage values. What can be the reason?
我试图从2015年1月1日运行循环到当前日期并填写数据库中每一天的价值。我使用朱利安日计数器作为循环变量。没有在循环内部我改变计数器的值但循环2次后计数器的值突然变为垃圾值。可能是什么原因?
for (julian_day_counter = 2457024; julian_day_counter < curr_day; \
julian_day_counter++)
{
for (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 for 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 is the gdb snippet:
以下是gdb片段:
Breakpoint 1, addr_db_update_dummy_appliance_consumption_profile (
p_glb_pdb=0x812a4b0, curr_day=2457151)
at 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)
at addr_db_app.c:13208
13208 for (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)
at addr_db_app.c:13208
13208 for (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 () from /lib/libc.so.6
2 个解决方案
#1
Well done to @undur_gongor for pointing this out in the comments to your question; the culprit is strncat()
. It's overwriting the buffer and corrupting other variables. You have a buffer overflow.
@undur_gongor做得很好,因为在你的问题的评论中指出了这一点;罪魁祸首是strncat()。它覆盖了缓冲区并破坏了其他变量。你有缓冲区溢出。
You don't appear to be passing the 3rd parameter correctly; from this question:
您似乎没有正确传递第3个参数;来自这个问题:
#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);
#2
The real clue here is:
这里真正的线索是:
0x00b6a33b in strncat () from /lib/libc.so.6
来自/lib/libc.so.6的strncat()中的0x00b6a33b
Your value is being modified in some other function call that leads to a strncat
. The offending code is not in the question as it stands now, however. :)
在导致strncat的其他函数调用中正在修改您的值。然而,违规代码不在现在的问题中。 :)
#1
Well done to @undur_gongor for pointing this out in the comments to your question; the culprit is strncat()
. It's overwriting the buffer and corrupting other variables. You have a buffer overflow.
@undur_gongor做得很好,因为在你的问题的评论中指出了这一点;罪魁祸首是strncat()。它覆盖了缓冲区并破坏了其他变量。你有缓冲区溢出。
You don't appear to be passing the 3rd parameter correctly; from this question:
您似乎没有正确传递第3个参数;来自这个问题:
#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);
#2
The real clue here is:
这里真正的线索是:
0x00b6a33b in strncat () from /lib/libc.so.6
来自/lib/libc.so.6的strncat()中的0x00b6a33b
Your value is being modified in some other function call that leads to a strncat
. The offending code is not in the question as it stands now, however. :)
在导致strncat的其他函数调用中正在修改您的值。然而,违规代码不在现在的问题中。 :)