From f5d44299163376415b0a5d837a34f2872d307afe Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 1 Oct 2007 02:47:44 +0000 Subject: [PATCH] Patch that: 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 --- include/opensrf/utils.h | 13 +++++++++++++ src/libopensrf/basic_client.c | 4 ++-- src/libopensrf/log.c | 2 +- src/libopensrf/osrf_legacy_json.c | 4 ++-- src/libopensrf/osrf_message.c | 2 +- src/libopensrf/osrf_prefork.c | 6 +++--- src/libopensrf/osrf_transgroup.c | 8 ++++---- src/libopensrf/socket_bundle.c | 4 ++-- src/libopensrf/transport_client.c | 2 +- src/libopensrf/transport_message.c | 5 ++++- src/libopensrf/utils.c | 26 ++++++++++++++++++-------- 11 files changed, 51 insertions(+), 25 deletions(-) diff --git a/include/opensrf/utils.h b/include/opensrf/utils.h index c6832cd..ebf05a5 100644 --- a/include/opensrf/utils.h +++ b/include/opensrf/utils.h @@ -41,6 +41,18 @@ GNU General Public License for more details. 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 {\ @@ -159,6 +171,7 @@ int set_proc_title( char* format, ... ); int daemonize(); void* safe_malloc(int size); +void* safe_calloc(int size); // --------------------------------------------------------------------------------- // Generic growing buffer. Add data all you want diff --git a/src/libopensrf/basic_client.c b/src/libopensrf/basic_client.c index 246c1b9..a477ab0 100644 --- a/src/libopensrf/basic_client.c +++ b/src/libopensrf/basic_client.c @@ -31,7 +31,7 @@ int main( int argc, char** argv ) { 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) ) { @@ -48,7 +48,7 @@ int main( int argc, char** argv ) { 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 ); diff --git a/src/libopensrf/log.c b/src/libopensrf/log.c index 744cdbd..8fb5094 100644 --- a/src/libopensrf/log.c +++ b/src/libopensrf/log.c @@ -221,7 +221,7 @@ static void _osrfLogDetail( int level, const char* filename, int line, char* msg 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] = '.'; diff --git a/src/libopensrf/osrf_legacy_json.c b/src/libopensrf/osrf_legacy_json.c index fbd3064..ae75588 100644 --- a/src/libopensrf/osrf_legacy_json.c +++ b/src/libopensrf/osrf_legacy_json.c @@ -480,7 +480,7 @@ int json_parse_json_string(char* string, unsigned long* index, jsonObject* obj, "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); @@ -693,7 +693,7 @@ int json_eat_comment(char* string, unsigned long* index, char** buffer, int pars 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 ); diff --git a/src/libopensrf/osrf_message.c b/src/libopensrf/osrf_message.c index c5ce04c..c7c7efc 100644 --- a/src/libopensrf/osrf_message.c +++ b/src/libopensrf/osrf_message.c @@ -175,7 +175,7 @@ jsonObject* osrfMessageToJSON( osrfMessage* msg ) { jsonObjectSetClass(json, "osrfMessage"); jsonObject* payload; char sc[64]; - memset(sc, 0, sizeof(sc)); + osrf_clearbuf(sc, sizeof(sc)); char* str; diff --git a/src/libopensrf/osrf_prefork.c b/src/libopensrf/osrf_prefork.c index a75ea3a..4d4b996 100644 --- a/src/libopensrf/osrf_prefork.c +++ b/src/libopensrf/osrf_prefork.c @@ -547,7 +547,7 @@ void check_children( prefork_simple* forker, int forever ) { /* 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); } @@ -566,7 +566,7 @@ void prefork_child_wait( prefork_child* child ) { 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++ ) { @@ -579,7 +579,7 @@ void prefork_child_wait( prefork_child* child ) { 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; } diff --git a/src/libopensrf/osrf_transgroup.c b/src/libopensrf/osrf_transgroup.c index 3c419c4..566267b 100644 --- a/src/libopensrf/osrf_transgroup.c +++ b/src/libopensrf/osrf_transgroup.c @@ -89,7 +89,7 @@ int osrfTransportGroupSendMatch( osrfTransportGroup* grp, transport_message* msg 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); @@ -108,15 +108,15 @@ int osrfTransportGroupSend( osrfTransportGroup* grp, transport_message* msg ) { 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; diff --git a/src/libopensrf/socket_bundle.c b/src/libopensrf/socket_bundle.c index 59f4c55..3a09132 100644 --- a/src/libopensrf/socket_bundle.c +++ b/src/libopensrf/socket_bundle.c @@ -707,7 +707,7 @@ static int _socket_handle_client_data(socket_manager* mgr, socket_node* node) { 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()); @@ -717,7 +717,7 @@ static int _socket_handle_client_data(socket_manager* mgr, socket_node* node) { 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() */ diff --git a/src/libopensrf/transport_client.c b/src/libopensrf/transport_client.c index 9a14291..5676a43 100644 --- a/src/libopensrf/transport_client.c +++ b/src/libopensrf/transport_client.c @@ -23,7 +23,7 @@ int main( int argc, char** argv ) { 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 { diff --git a/src/libopensrf/transport_message.c b/src/libopensrf/transport_message.c index 6488115..80b1ea9 100644 --- a/src/libopensrf/transport_message.c +++ b/src/libopensrf/transport_message.c @@ -238,7 +238,7 @@ char* message_to_xml( const transport_message* msg ) { 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 ); } @@ -303,6 +303,7 @@ void jid_get_username( const char* jid, char buf[], int size ) { if( jid[i] == 64 ) { /*ascii @*/ if(i > size) i = size; strncpy( buf, jid, i ); + buf[i] = '\0'; // strncpy doesn't provide the nul return; } } @@ -319,6 +320,7 @@ void jid_get_resource( const char* jid, char buf[], int size) { int rlen = len - (i+1); if(rlen > size) rlen = size; strncpy( buf, start, rlen ); + buf[rlen] = '\0'; // strncpy doesn't provide the nul } } } @@ -343,6 +345,7 @@ void jid_get_domain( const char* jid, char buf[], int size ) { int dlen = index2 - index1; if(dlen > size) dlen = size; memcpy( buf, jid + index1, dlen ); + buf[dlen] = '\0'; // memcpy doesn't provide the nul } } diff --git a/src/libopensrf/utils.c b/src/libopensrf/utils.c index 987a527..0323fb7 100644 --- a/src/libopensrf/utils.c +++ b/src/libopensrf/utils.c @@ -17,6 +17,16 @@ GNU General Public License for more details. #include inline void* safe_malloc( int size ) { + void* ptr = (void*) malloc( size ); + if( ptr == NULL ) { + 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" ); @@ -45,7 +55,7 @@ int init_proc_title( int argc, char* argv[] ) { 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++; } @@ -56,7 +66,7 @@ int init_proc_title( int argc, char* argv[] ) { 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 ); } @@ -122,7 +132,7 @@ char* va_list_to_string(const char* format, ...) { 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); @@ -165,7 +175,7 @@ int buffer_fadd(growing_buffer* gb, const char* format, ... ) { 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); @@ -213,7 +223,7 @@ int buffer_add(growing_buffer* gb, char* data) { 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; } @@ -417,7 +427,7 @@ char* file_to_string(const char* filename) { 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"); @@ -428,7 +438,7 @@ char* file_to_string(const char* filename) { while(fgets(buf, sizeof(buf), file)) { buffer_add(gb, buf); - memset(buf, 0, sizeof(buf)); + osrf_clearbuf(buf, sizeof(buf)); } fclose(file); @@ -456,7 +466,7 @@ char* md5sum( char* text, ... ) { 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]); -- 2.43.2