String & NULL patch

Steve G <[email protected]> Thu, 29 Jan 2004 08:49:44 -0800 (PST)
Newsgroups gmane.network.zeroconf.workers
Message-ID <[email protected]>
Hello,

This is the last of the "C" syntax patches. It cleans up places
that use &string. In dns.c, last_dnptr was being set 1 past the
end of the array. And when malloc fails, return a NULL pointer. 

I am thinking that an out of memory function should be created
that tries to do a syslog and then exits. I'll come back to this
later unless someone else wants to put one in.

My next step will be to start a dynamic analysis of tmdns.

Sincerely,
Steve Grubb

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/
tmdns-patch4.txt (text/plain, 8.2 KB)
diff -urBb tmdns-devel-c/server/conf.c tmdns-devel/server/conf.c
--- tmdns-devel-c/server/conf.c	2004-01-29 10:20:42.000000000 -0500
+++ tmdns-devel/server/conf.c	2004-01-29 11:00:49.000000000 -0500
@@ -84,8 +84,8 @@
      "# the advertised name.\n"
      "# Note that only the part up to the first dot (if any) is used as hostname.\n"
      "#.\n",
-     &config.hostname,
-     &config_defaults.hostname,
+     config.hostname,
+     config_defaults.hostname,
      copy_string ,
      copy_string ,
      print_string
@@ -93,8 +93,8 @@
     { 
      "username" ,
      "# if not empty, tmdns will run as this user.\n",
-     &config.username,
-     &config_defaults.username,
+     config.username,
+     config_defaults.username,
      copy_string ,
      copy_string ,
      print_string
@@ -108,8 +108,8 @@
      "# Some distributions may support automatic service registration in the services\n" 
      "# 'init' scripts, in which case you can leave this setting (and the file) alone.\n" 
      "# \n" ,
-     &config.service_file,
-     &config_defaults.service_file,
+     config.service_file,
+     config_defaults.service_file,
      copy_string ,
      copy_string ,
      print_string
@@ -117,8 +117,8 @@
    { 
      "pid_file" ,
      "# tmdns will save its pid in that file\n" ,
-     &config.pid_file,
-     &config_defaults.pid_file,
+     config.pid_file,
+     config_defaults.pid_file,
      copy_string ,
      copy_string ,
      print_string
@@ -127,8 +127,8 @@
      "debug_file" ,
      "# Debug info log file\n" 
      "# If you want tmdns to log debug info, specify a file here.\n",
-     &config.debug_file,
-     &config_defaults.debug_file,
+     config.debug_file,
+     config_defaults.debug_file,
      copy_string ,
      copy_string ,
      print_string
@@ -187,8 +187,8 @@
      "# Do not use this parameter if there is a real DNS for a domain and that nameserver\n"
      "# does not support multicast dns.\n"
      "# \n" ,
-     &config.also_local,
-     &config_defaults.also_local,
+     config.also_local,
+     config_defaults.also_local,
      init_string_array,
      copy_also_local,
      print_string_array
@@ -227,8 +227,8 @@
      "# You have been warned !\n" 
      "# \n" 
      "# \n" ,
-     &config.dynamic_service_file,
-     &config_defaults.dynamic_service_file,
+     config.dynamic_service_file,
+     config_defaults.dynamic_service_file,
      copy_string ,
      copy_string ,
      print_string
diff -urBb tmdns-devel-c/server/dns.c tmdns-devel/server/dns.c
--- tmdns-devel-c/server/dns.c	2004-01-29 10:01:41.000000000 -0500
+++ tmdns-devel/server/dns.c	2004-01-29 10:54:34.000000000 -0500
@@ -41,7 +41,7 @@
     pkg->bufsize    = DNSDATASIZE;
     pkg->data       = pkg->u.raw + sizeof(HEADER);
     pkg->dnptrs[0]  = pkg->data;
-    pkg->last_dnptr = pkg->dnptrs[MAXDOMAINS];
+    pkg->last_dnptr = pkg->dnptrs[MAXDOMAINS-1];
 
 }
 /****************************************************************************
diff -urBb tmdns-devel-c/server/info.c tmdns-devel/server/info.c
--- tmdns-devel-c/server/info.c	2004-01-29 10:23:11.000000000 -0500
+++ tmdns-devel/server/info.c	2004-01-29 11:27:57.000000000 -0500
@@ -5,7 +5,6 @@
 
 #include <sys/utsname.h>
 #include <net/if.h>
-#include <sys/ioctl.h>
 #include <netinet/in.h>
 #include <ctype.h>
 #include <assert.h>
@@ -138,6 +137,8 @@
     dns_rr * aRec = NULL;
     
     aRec = (dns_rr *)malloc( sizeof(dns_rr) );
+    if (aRec == NULL)
+        return NULL;
     init_rr(aRec,domain,T_A);
     memcpy(&(aRec->rr.a),ip,sizeof(struct in_addr));
 
@@ -159,6 +160,8 @@
     dns_rr * ptrRec = NULL;
     
     ptrRec = (dns_rr *)malloc( sizeof(dns_rr) );
+    if (ptrRec == NULL)
+        return NULL;
     init_rr(ptrRec,rev,T_PTR);
     ptrRec->rr.dn = strdup(domain);
 
@@ -185,6 +188,8 @@
     dns_rr * srvRec = NULL;
 
     srvRec = (dns_rr *)malloc( sizeof(dns_rr) );
+    if (srvRec == NULL)
+        return NULL;
     init_rr(srvRec,domain,T_SRV);
 
     srvRec->rr.srv.target   = strdup(target);
@@ -257,7 +262,7 @@
 
 	/* semantic check ... */
 
-	if( *name != 0 ) {
+	if(name && (*name != 0) ) {
 	    char * escaped = NULL;
 	    escaped = escapeDomainLabel(name);
 	    if( strlen(escaped) > 63 ) {
@@ -312,7 +317,7 @@
 
     uname(&info);
 
-    if( ( config.hostname != NULL ) && (  config.hostname[0] != 0 ) ) {
+    if( config.hostname[0] != 0 ) {
         raw_namebuf = strdup(config.hostname);
     } else {
         raw_namebuf = strdup(info.nodename);
@@ -448,14 +453,9 @@
  *****************************************************************************/
 int info_search( search_state * state ) {
 
-    const char * dname;
-    short type;
     ll_entry_t * startAt = NULL;
     ll_entry_t * el      = NULL;
 
-    dname   = state->dname;
-    type    = state->type;
-
     if( state->el == NULL ) {
 	startAt = ll_first(records);
     } else {
diff -urBb tmdns-devel-c/server/serv_udp.c tmdns-devel/server/serv_udp.c
--- tmdns-devel-c/server/serv_udp.c	2004-01-29 10:07:13.000000000 -0500
+++ tmdns-devel/server/serv_udp.c	2004-01-29 11:13:01.000000000 -0500
@@ -131,6 +131,7 @@
 		break;
 	
 	    case AF_INET6:
+            default:
 		break;
 	}
     }
@@ -237,7 +238,7 @@
   /* Read in the actual packet */
   udp_pkt->src_len = sizeof(struct sockaddr);
 
-  if ((numread = recvfrom(sockfd, &udp_pkt->buf, sizeof(udp_pkt->buf),0,
+  if ((numread = recvfrom(sockfd, udp_pkt->buf, sizeof(udp_pkt->buf),0,
 	&(udp_pkt->src_address), &(udp_pkt->src_len) )) < 0) {
     debug_perror("udp_packet_read: recvfrom");
 
@@ -267,7 +268,7 @@
    */
   if( pkt->from_mcast || pkt->dst_port < 0 ) {
       sendto( mcast_sock, 
-	      &pkt->u.raw , dns_get_len(pkt), 
+	      pkt->u.raw , dns_get_len(pkt), 
 	      0 , 
 	      &mcast_v4_sa , sizeof(mcast_v4_sa));
 
@@ -286,7 +287,7 @@
       /* Zero socket address */
       memset((void *)&sa, 0, sizeof(sa));
 
-      bytes = sendto(sockfd, &pkt->u.raw , dns_get_len(pkt), 
+      bytes = sendto(sockfd, pkt->u.raw , dns_get_len(pkt), 
 	      0, &(pkt->dst_address), pkt->dst_len );
 
       debug("sent %d(of %d) bytes to %s\n" , 
diff -urBb tmdns-devel-c/server/tmdns.c tmdns-devel/server/tmdns.c
--- tmdns-devel-c/server/tmdns.c	2004-01-29 09:56:55.000000000 -0500
+++ tmdns-devel/server/tmdns.c	2004-01-29 11:22:07.000000000 -0500
@@ -191,7 +191,7 @@
 
     struct passwd * pw = NULL;
 
-    if( (config.username == NULL ) || (config.username[0] == 0 )) {
+    if( config.username[0] == 0 ) {
 	debug("no username given\n");
 	return;
     }
@@ -203,13 +203,13 @@
     }
 
     /* change ownership of the pid file so we can unlink it later */
-    if( (config.pid_file ) != NULL && (config.pid_file[0] != 0) ) {
+    if( config.pid_file[0] != 0) {
 	debug("set ownership of the pid file %s", config.pid_file );
 	chown( config.debug_file , pw->pw_uid, pw->pw_gid );
     }
 
     /* change ownership of the log file */
-    if( (config.debug_file ) != NULL && (config.debug_file[0] != 0) ) {
+    if( config.debug_file[0] != 0) {
 	debug("set ownership of the debug file %s", config.debug_file );
 	chown( config.debug_file , pw->pw_uid, pw->pw_gid );
     }
@@ -436,7 +436,7 @@
 
 		/* not probing */
 
-		if( ((HEADER *)&(pkt.buf))->qr == 0 ) {
+		if( ((HEADER *)pkt.buf)->qr == 0 ) {
 
 		    /* packet is a query */
 
@@ -466,7 +466,7 @@
 
 		/* probing for dups */
 
-		if( ((HEADER *)&(pkt.buf))->qr == 1 ) 
+		if( ((HEADER *)pkt.buf)->qr == 1 ) 
 		{
 		    /* got an answer while probing*/
 
@@ -662,7 +662,8 @@
     }
     
     answer = (dns_t *)malloc( sizeof(dns_t) );
-
+    if (answer == NULL)
+        exit(1);
     dns_init(answer);
 
     debug_dns("query from client", &(udp_pkt->buf));
@@ -748,7 +749,8 @@
     {
 	stored_query * sq = (stored_query *)malloc(sizeof(stored_query));
 	dns_query * qp    = (dns_query *)malloc(sizeof(dns_query));
-
+        if (sq && qp)
+        {
 	memcpy(qp,query,sizeof(dns_query));
 
 	sq->answer = answer;
@@ -756,6 +758,7 @@
 
         ll_add(query_list,sq);
     }
+    }
 
 
     debug("init mcast query ...\n");
@@ -872,8 +875,13 @@
     dns_t * answer    = ((stored_query *)user_data)->answer;
     dns_query * query = ((stored_query *)user_data)->query;
     
+#ifdef DEBUG    
     assert(query  != NULL );
     assert(answer != NULL );
+#else
+    if ((query == NULL) || (answer == NULL))
+        exit(1); /* FIXME: syslog */
+#endif
 
     if( section != IN_ANSWER_SECT ) { return 0; }