[Opensrf-commits] r1099 - in trunk: include/opensrf src/libopensrf

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Sep 30 22:58:15 EDT 2007


Author: miker
Date: 2007-09-30 22:47:44 -0400 (Sun, 30 Sep 2007)
New Revision: 1099

Modified:
   trunk/include/opensrf/utils.h
   trunk/src/libopensrf/basic_client.c
   trunk/src/libopensrf/log.c
   trunk/src/libopensrf/osrf_legacy_json.c
   trunk/src/libopensrf/osrf_message.c
   trunk/src/libopensrf/osrf_prefork.c
   trunk/src/libopensrf/osrf_transgroup.c
   trunk/src/libopensrf/socket_bundle.c
   trunk/src/libopensrf/transport_client.c
   trunk/src/libopensrf/transport_message.c
   trunk/src/libopensrf/utils.c
Log:
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.



Modified: trunk/include/opensrf/utils.h
===================================================================
--- trunk/include/opensrf/utils.h	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/include/opensrf/utils.h	2007-10-01 02:47:44 UTC (rev 1099)
@@ -41,6 +41,18 @@
 		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 daemonize();
 
 void* safe_malloc(int size);
+void* safe_calloc(int size);
 
 // ---------------------------------------------------------------------------------
 // Generic growing buffer. Add data all you want

Modified: trunk/src/libopensrf/basic_client.c
===================================================================
--- trunk/src/libopensrf/basic_client.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/basic_client.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -31,7 +31,7 @@
 		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 @@
 			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 );

Modified: trunk/src/libopensrf/log.c
===================================================================
--- trunk/src/libopensrf/log.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/log.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -221,7 +221,7 @@
 
    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] = '.';

Modified: trunk/src/libopensrf/osrf_legacy_json.c
===================================================================
--- trunk/src/libopensrf/osrf_legacy_json.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/osrf_legacy_json.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -480,7 +480,7 @@
 							"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_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 );

Modified: trunk/src/libopensrf/osrf_message.c
===================================================================
--- trunk/src/libopensrf/osrf_message.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/osrf_message.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -175,7 +175,7 @@
 	jsonObjectSetClass(json, "osrfMessage");
 	jsonObject* payload;
 	char sc[64];
-	memset(sc, 0, sizeof(sc));
+	osrf_clearbuf(sc, sizeof(sc));
 
 	char* str;
 

Modified: trunk/src/libopensrf/osrf_prefork.c
===================================================================
--- trunk/src/libopensrf/osrf_prefork.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/osrf_prefork.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -547,7 +547,7 @@
 
 			/* 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 @@
 	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 @@
 			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;
 		}
 

Modified: trunk/src/libopensrf/osrf_transgroup.c
===================================================================
--- trunk/src/libopensrf/osrf_transgroup.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/osrf_transgroup.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -89,7 +89,7 @@
 	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 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;

Modified: trunk/src/libopensrf/socket_bundle.c
===================================================================
--- trunk/src/libopensrf/socket_bundle.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/socket_bundle.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -707,7 +707,7 @@
 	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 @@
 		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() */
 

Modified: trunk/src/libopensrf/transport_client.c
===================================================================
--- trunk/src/libopensrf/transport_client.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/transport_client.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -23,7 +23,7 @@
 		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 {

Modified: trunk/src/libopensrf/transport_message.c
===================================================================
--- trunk/src/libopensrf/transport_message.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/transport_message.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -238,7 +238,7 @@
 		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 @@
 		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 @@
 			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 @@
 		int dlen = index2 - index1;
 		if(dlen > size) dlen = size;
 		memcpy( buf, jid + index1, dlen );
+		buf[dlen] = '\0'; // memcpy doesn't provide the nul
 	}
 }
 

Modified: trunk/src/libopensrf/utils.c
===================================================================
--- trunk/src/libopensrf/utils.c	2007-09-30 19:14:26 UTC (rev 1098)
+++ trunk/src/libopensrf/utils.c	2007-10-01 02:47:44 UTC (rev 1099)
@@ -22,6 +22,16 @@
 		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;
 }
@@ -45,7 +55,7 @@
 	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 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 @@
 	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 @@
 	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_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 @@
 
 	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 @@
 
 	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 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]);



More information about the opensrf-commits mailing list