08-12-2014 05:15 AM
Hello,
I have defined a structure in the program :
typedef struct
{
char name[100];
char ip_addr[65];
...;
}BATTERY_ALARM_STATE;
static BATTERY_ALARM_STATE gBatteryAlarmState;
void func()
{
...;
snprintf(gBatteryAlarmState.ip_addr, sizeof(gBatteryAlarmState.ip_addr), "%s", "127.0.0.1");
snprintf(gBatteryAlarmState.name, sizeof(gBatteryAlarmState.name), "%s", "123456");
}
when executing the func(), it always popup the error message: Attempt to write beyond end of string. and the error always be stopped at the second "snprintf". This code run well in the cvi2013, but just have the error in the cvi 2013 sp1.
David
08-12-2014 09:43 PM
A bit strange.
Try snprintf(gBatteryAlarmState.name, sizeof(gBatteryAlarmState.name), "%s", "1");
Will it popup the error message?
08-13-2014 10:25 PM
yes, it also popup the error. if add a dummy string array in the structure, then access the name array element, the error will disappeared.
08-13-2014 10:27 PM
So no any idea, I have to use the strncpy() instead of that.
08-14-2014 10:44 AM - edited 08-14-2014 10:48 AM
Hi David,
Where do you call func
, in your program? I tried putting together a simple application (see snippet that follows), but it doesn't reproduce the behavior you described in LabWindows/CVI 2013 SP1.
// Include files #include <ansi_c.h> //==================================================================================================== // Types typedef struct { char name[100]; char ip_addr[65]; }BATTERY_ALARM_STATE; //==================================================================================================== // Static global variables static BATTERY_ALARM_STATE gBatteryAlarmState; //==================================================================================================== // Function declarations void func(); //==================================================================================================== // Global functions int main () { func(); return 0; } void func() { snprintf(gBatteryAlarmState.ip_addr, sizeof(gBatteryAlarmState.ip_addr), "%s", "127.0.0.1"); snprintf(gBatteryAlarmState.name, sizeof(gBatteryAlarmState.name), "%s", "123456"); } //====================================================================================================
Can you confirm whether this code causes the same error for you?
Thanks,