1) Creates safe_calloc, suggested by Scott McKellar which includes
memset, as calloc does. safe_malloc will eventually lose its call
to memset.
2) Creates a macro in utils.h called osrf_clearbuf which
* under CLFAGS=-DNDEBUG fills the buffer with !s and a trailing nul
* otherwise (currently) uses memset to fill with nuls
The secondary behavior should be changed to a no-op after no more
problems arise under NDEBUG mode. I reversed the suggested semantics
because I'm not ready to completely break trunk. To break everything
(AKA find where we should be providing a terminal nul) just compile
like this:
$ CLFAGS=-DNDEBUG make clean all
3) replaces all memsets (excepting the ones in safe_?alloc) that act
on char bufs with said macro, so they can be spotted and improved to
deal with nul terminators where needed. I didn't touch any memsets on
struct pointers.
4) made jid_get_*() from src/libopensrf/transport_message.c safe for
use with NDEBUG mode. They were depending on the target buffer that
the caller passes in to be nul-filled (they use strncpy/memcpy which
don't guarantee terminal nul) -- now they provide their own terminal
nul.
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1099
9efc2488-bf62-4759-914b-
345cdb29e865
memset( ptr, 0, size );\
} while(0)
+#ifndef NDEBUG
+// The original ... replace with noop once no more errors occur in NDEBUG mode
+#define osrf_clearbuf( s, n ) memset( s, 0, n )
+#else
+#define osrf_clearbuf( s, n ) \
+ do { \
+ char * clearbuf_temp_s = (s); \
+ size_t clearbuf_temp_n = (n); \
+ memset( clearbuf_temp_s, '!', clearbuf_temp_n ); \
+ clearbuf_temp_s[ clearbuf_temp_n - 1 ] = '\0'; \
+ } while( 0 )
+#endif
#define OSRF_BUFFER_ADD(gb, data) \
do {\
int daemonize();
void* safe_malloc(int size);
+void* safe_calloc(int size);
// ---------------------------------------------------------------------------------
// Generic growing buffer. Add data all you want
signal(SIGINT, sig_int);
fprintf(stderr, "Listener: %ld\n", (long) getpid() );
char buf[300];
- memset(buf, 0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
printf("=> ");
while( fgets( buf, sizeof(buf), stdin) ) {
client_send_message( client, send );
message_free( send );
printf("\n=> ");
- memset(buf, 0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
}
fprintf(stderr, "Killing child %d\n", pid );
kill( pid, SIGKILL );
if( logtype == OSRF_LOG_TYPE_SYSLOG ) {
char buf[1536];
- memset(buf, 0x0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
/* give syslog some breathing room, and be cute about it */
strncat(buf, msg, 1535);
buf[1532] = '.';
"json_parse_json_string(): truncated escaped unicode"); }
char buff[5];
- memset(buff, 0, sizeof(buff));
+ osrf_clearbuf(buff, sizeof(buff));
memcpy(buff, string + (*index), 4);
int json_handle_error(char* string, unsigned long* index, char* err_msg) {
char buf[60];
- memset(buf, 0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
if(*index > 30)
strncpy( buf, string + (*index - 30), 59 );
jsonObjectSetClass(json, "osrfMessage");
jsonObject* payload;
char sc[64];
- memset(sc, 0, sizeof(sc));
+ osrf_clearbuf(sc, sizeof(sc));
char* str;
/* now suck off the data */
char buf[64];
- memset( buf, 0, sizeof(buf) );
+ osrf_clearbuf( buf, sizeof(buf) );
if( (n=read(cur_child->read_status_fd, buf, 63)) < 0 ) {
osrfLogWarning( OSRF_LOG_MARK, "Read error after select in child status read with errno %d", errno);
}
int i,n;
growing_buffer* gbuf = buffer_init( READ_BUFSIZE );
char buf[READ_BUFSIZE];
- memset( buf, 0, sizeof(buf) );
+ osrf_clearbuf( buf, sizeof(buf) );
for( i = 0; i < child->max_requests; i++ ) {
if(!gotdata)
set_fl(child->read_data_fd, O_NONBLOCK );
buffer_add( gbuf, buf );
- memset( buf, 0, sizeof(buf) );
+ osrf_clearbuf( buf, sizeof(buf) );
gotdata = 1;
}
if(!(grp && msg)) return -1;
char domain[256];
- memset(domain, 0, sizeof(domain));
+ osrf_clearbuf(domain, sizeof(domain));
jid_get_domain( msg->recipient, domain, 255 );
osrfTransportGroupNode* node = osrfHashGet(grp->nodes, domain);
int bufsize = 256;
char domain[bufsize];
- memset(domain, 0, sizeof(domain));
+ osrf_clearbuf(domain, sizeof(domain));
jid_get_domain( msg->recipient, domain, bufsize - 1 );
char msgrecip[bufsize];
- memset(msgrecip, 0, sizeof(msgrecip));
+ osrf_clearbuf(msgrecip, sizeof(msgrecip));
jid_get_username(msg->recipient, msgrecip, bufsize - 1);
char msgres[bufsize];
- memset(msgres, 0, sizeof(msgres));
+ osrf_clearbuf(msgres, sizeof(msgres));
jid_get_resource(msg->recipient, msgres, bufsize - 1);
char* firstdomain = NULL;
int read_bytes;
int sock_fd = node->sock_fd;
- memset(buf, 0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
set_fl(sock_fd, O_NONBLOCK);
osrfLogInternal( OSRF_LOG_MARK, "%ld : Received data at %f\n", (long) getpid(), get_timestamp_millis());
if(mgr->data_received)
mgr->data_received(mgr->blob, mgr, sock_fd, buf, node->parent_id);
- memset(buf, 0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
}
int local_errno = errno; /* capture errno as set by recv() */
if( recv->body ) {
int len = strlen(recv->body);
char buf[len + 20];
- memset( buf, 0, len + 20);
+ osrf_clearbuf( buf, 0, sizeof(buf));
sprintf( buf, "Echoing...%s", recv->body );
send = message_init( buf, "Echoing Stuff", "12345", recv->sender, "" );
} else {
xmlAddChild( message_node, error_node );
xmlNewProp( error_node, BAD_CAST "type", BAD_CAST msg->error_type );
char code_buf[16];
- memset( code_buf, 0, 16);
+ osrf_clearbuf( code_buf, sizeof(code_buf));
sprintf(code_buf, "%d", msg->error_code );
xmlNewProp( error_node, BAD_CAST "code", BAD_CAST code_buf );
}
if( jid[i] == 64 ) { /*ascii @*/
if(i > size) i = size;
strncpy( buf, jid, i );
+ buf[i] = '\0'; // strncpy doesn't provide the nul
return;
}
}
int rlen = len - (i+1);
if(rlen > size) rlen = size;
strncpy( buf, start, rlen );
+ buf[rlen] = '\0'; // strncpy doesn't provide the nul
}
}
}
int dlen = index2 - index1;
if(dlen > size) dlen = size;
memcpy( buf, jid + index1, dlen );
+ buf[dlen] = '\0'; // memcpy doesn't provide the nul
}
}
osrfLogError( OSRF_LOG_MARK, "Out of Memory" );
exit(99);
}
+ memset( ptr, 0, size ); // remove this after safe_calloc transition
+ return ptr;
+}
+
+inline void* safe_calloc( int size ) {
+ void* ptr = (void*) malloc( size );
+ if( ptr == NULL ) {
+ osrfLogError( OSRF_LOG_MARK, "Out of Memory" );
+ exit(99);
+ }
memset( ptr, 0, size );
return ptr;
}
int i = 0;
while( i < argc ) {
int len = strlen( global_argv[i]);
- memset( global_argv[i], 0, len);
+ osrf_clearbuf( global_argv[i], len );
global_argv_size += len;
i++;
}
int set_proc_title( char* format, ... ) {
VA_LIST_TO_STRING(format);
- memset( *(global_argv), 0, global_argv_size);
+ osrf_clearbuf( *(global_argv), global_argv_size);
return snprintf( *(global_argv), global_argv_size, VA_BUF );
}
len = va_list_size(format, args);
char buf[len];
- memset(buf, 0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
va_start(a_copy, format);
vsnprintf(buf, len - 1, format, a_copy);
len = va_list_size(format, args);
char buf[len];
- memset(buf, 0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
va_start(a_copy, format);
vsnprintf(buf, len - 1, format, a_copy);
int buffer_reset( growing_buffer *gb){
if( gb == NULL ) { return 0; }
if( gb->buf == NULL ) { return 0; }
- memset( gb->buf, 0, sizeof(gb->buf) );
+ osrf_clearbuf( gb->buf, sizeof(gb->buf) );
gb->n_used = 0;
return 1;
}
int len = 1024;
char buf[len];
- memset(buf, 0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
growing_buffer* gb = buffer_init(len);
FILE* file = fopen(filename, "r");
while(fgets(buf, sizeof(buf), file)) {
buffer_add(gb, buf);
- memset(buf, 0, sizeof(buf));
+ osrf_clearbuf(buf, sizeof(buf));
}
fclose(file);
char buf[16];
char final[256];
- memset(final, 0, sizeof(final));
+ osrf_clearbuf(final, sizeof(final));
for ( i=0 ; i<16 ; i++ ) {
snprintf(buf, sizeof(buf), "%02x", digest[i]);