int a[2];
…
a[2] = 27; // Invalid…. a[0] and a[1] are the two elements.
char *tmp[20];
…
sprintf(tmp, "Some stuff to print for %s.", "a longer string that you wanted");
char *tmp[20];
…
sprintf(tmp, "Some stuff to print for %s.", "a longer string that you wanted");
char *tmp[20];
…
sprintf(tmp, "Some stuff to print for %s.", "a longer string that you wanted");
char * func() {
char tmp[256];
bzero(tmp, 256);
if( snprintf( tmp + strlen( tmp ), sizeof( tmp ) - strlen( tmp ) - 1, "Stuff 1 %s", foo1 ) >= sizeof( tmp ) - strlen( tmp ) - 1) {
// string truncated
return( "error");
}
if( snprintf( tmp + strlen( tmp ), sizeof( tmp ) - strlen( tmp ) - 1, " and Stuff 2 %s", bar2 ) >= sizeof( tmp ) - strlen( tmp ) - 1) {
// string truncated
return( "error" );
}
return( tmp );
}
char * func() {
char *tmp;
char *spot;
int would_write;
int allocated;
int offset;
int difference;
allocated = 256;
if( !( tmp = calloc( sizeof( char *), allocated ) ) {
// no memory
return( strdup( "error" ) );
}
for( spot = tmp + strlen( tmp ); would_write = snprintf( spot, allocated - (spot - tmp) - 1, "Stuff 1 %s", foo1 ) >= allocated - (spot - tmp) - 1; ) {
// string truncated
offset = (spot - tmp);
difference = would_write - ( allocated - offset -1 );
if( !( tmp = realloc( tmp, allocated + difference + 1) ) ) {
// no memory
return( strdup( "error" ) );
}
allocated = allocated + difference + 1;
spot = (tmp + offset);
}
for( spot = tmp + strlen( tmp ); would_write = snprintf( spot, allocated - (spot - tmp) - 1, " and Stuff 2 %s", bar2 ) >= allocated - (spot - tmp) - 1; ) {
// string truncated
offset = (spot - tmp);
difference = would_write - ( allocated - offset -1 );
if( !( tmp = realloc( tmp, allocated + difference + 1) ) ) {
// no memory
return( strdup( "error" ) );
}
allocated = allocated + difference + 1;
spot = (tmp + offset);
}
return( tmp );
}
I have a bug that randomly appears, I know why, but problem is as I still have not find HOW (yeah lack of unit testing is a problem) to reproduce it.
Basically, depending on the connection/deconnection/reconnection of a character, if the is grouped (in a perticular way) at one point, I nullify a pointer before removing it from the "group" linked list. (and probably some other, but it is this one that makes it crash at least)
I know that, Valgrind tells me pointer get dereferenced. But the problem is, as it does not crash right away, I have a hard time finding what logic is missing, so I was wondering, is there a way to know exactly WHEN a list go wrong.
Is there not a way to watch a variable and stop it when it becomes undefined in gdb ? I know I can watch variable and stop it on some conditions, but not on an 'undefined' value..very annoying.
Hoping I just miss something and you could help.