Re: [PATCH] Code cleanup (warning fixes)

John Lauro <[email protected]> Tue, 15 Oct 2013 14:00:50 -0400 (EDT)
Newsgroups gmane.network.poptop
Message-ID <[email protected]>
This include my previous PATCH to "Use unsigned types to handle negative error codes".

In addition,
Fix several warnings in newer compilers, caused by format and type discrepancies, and
comparing with == directly to "", as "" is not guaranteed to always be the same when redefined.
Switch a copy function to strncpy, as original had a potential off by one error as the size check didn't account for trailing 0 to be added in next line.

Still several warnings from gcc left, but far fewer now...


> Description: Fix several warnings and minor issues
> codes
> Author: [email protected]
> 


--- a/bcrelay.c
+++ b/bcrelay.c
@@ -212,10 +212,11 @@ static int vdaemon = 0;
 #define NVBCR_PRINTF( args ) \
  if ((vdaemon == 0) && (do_org_info_printfs == 1)) printf args
 
+static char empty[1] = "";
 static char interfaces[32];
 static char log_interfaces[MAX_IFLOGTOSTR*MAXIF];
 static char log_relayed[(MAX_IFLOGTOSTR-1)*MAXIF+81];
-static char *ipsec = "";
+static char *ipsec = empty;
 
 static void showusage(char *prog)
 {
@@ -310,8 +311,8 @@ int main(int argc, char **argv) {
   regex_t preg;
   /* command line options */
   int c;
-  char *ifout = "";
-  char *ifin = "";
+  char *ifout = empty;
+  char *ifin = empty;
 
 #ifndef BCRELAY
   fprintf(stderr,
@@ -381,12 +382,12 @@ int main(int argc, char **argv) {
                         return 1;
                 }
   }
-  if (ifin == "") {
+  if (ifin == empty) {
        syslog(LOG_INFO,"Incoming interface required!");
        showusage(argv[0]);
        _exit(1);
   }
-  if (ifout == "" && ipsec == "") {
+  if (ifout == empty && ipsec == empty) {
        syslog(LOG_INFO,"Listen-mode or outgoing or IPsec interface required!");
        showusage(argv[0]);
        _exit(1);
@@ -431,7 +432,7 @@ static void mainloop(int argc, char **argv)
   static struct ifsnr old_ifsnr[MAXIF+1]; // Old iflist to socket fd's mapping list
   static struct ifsnr cur_ifsnr[MAXIF+1]; // Current iflist to socket fd's mapping list
   unsigned char buf[1518];
-  char *logstr = "";
+  char *logstr = empty;
 
   no_discifs_cntr = MAX_NODISCOVER_IFS;
   ifs_change = 0;
@@ -773,7 +774,7 @@ struct iflist *
 discoverActiveInterfaces(int s) {
   static struct iflist iflist[MAXIF+1];         // Allow for MAXIF interfaces
   static struct ifconf ifs;
-  int i, j, cntr = 0;
+  int i, cntr = 0;
   regex_t preg;
   struct ifreq ifrflags, ifr;
   struct sockaddr_in *sin;
@@ -816,9 +817,8 @@ discoverActiveInterfaces(int s) {
         /*
          * Get interface name
          */
-        for (j=0; (j<sizeof(iflist[cntr].ifname) && j<strlen(ifs.ifc_req[i].ifr_ifrn.ifrn_name)); ++j)
-                iflist[cntr].ifname[j] = ifs.ifc_req[i].ifr_ifrn.ifrn_name[j];
-        iflist[cntr].ifname[j+1] = '\0';
+        strncpy(iflist[cntr].ifname,ifs.ifc_req[i].ifr_ifrn.ifrn_name,sizeof(iflist[cntr].ifname));
+        iflist[cntr].ifname[sizeof(iflist[cntr].ifname)-1]=0;
 
         /*
          * Get local IP address 
@@ -870,7 +870,7 @@ discoverActiveInterfaces(int s) {
     // IPSEC tunnels are a fun one.  We must change the destination address
     // so that it will be routed to the correct tunnel end point.
     // We can define several tunnel end points for the same ipsec interface.
-    } else if (ipsec != "" && strncmp(ifs.ifc_req[i].ifr_name, "ipsec", 5) == 0) {
+    } else if (ipsec != empty && strncmp(ifs.ifc_req[i].ifr_name, "ipsec", 5) == 0) {
       if (strncmp(ifs.ifc_req[i].ifr_name, ipsec, 6) == 0) {
         struct hostent *hp = gethostbyname(ipsec+7);
         ioctl(s, SIOCGIFINDEX, &ifs.ifc_req[i]);
--- a/ctrlpacket.c
+++ b/ctrlpacket.c
@@ -69,7 +69,7 @@ static int make_out_call_rqst(unsigned char *rply_packet, ssize_t * rply_size);
 int read_pptp_packet(int clientFd, unsigned char *packet, unsigned char *rply_packet, ssize_t * rply_size)
 {
 
-       size_t bytes_read;
+       ssize_t bytes_read;
        int pptp_ctrl_type = 0; /* Control Message Type */
 
        /* read a packet and parse header */
@@ -133,10 +133,10 @@ int read_pptp_packet(int clientFd, unsigned char *packet, unsigned char *rply_pa
  * retn:        Number of bytes written on success.
  *              -1 on write failure.
  */
-size_t send_pptp_packet(int clientFd, unsigned char *packet, size_t packet_size)
+ssize_t send_pptp_packet(int clientFd, unsigned char *packet, size_t packet_size)
 {
 
-       size_t bytes_written;
+       ssize_t bytes_written;
 
        if ((bytes_written = write(clientFd, packet, packet_size)) == -1) {
                /* write failed */
@@ -146,7 +146,7 @@ size_t send_pptp_packet(int clientFd, unsigned char *packet, size_t packet_size)
        } else {
                /* debugging */
                if (pptpctrl_debug) {
-                       syslog(LOG_DEBUG, "CTRL: I wrote %d bytes to the client.", packet_size);
+                       syslog(LOG_DEBUG, "CTRL: I wrote %lu bytes to the client.", (unsigned long) packet_size);
                        syslog(LOG_DEBUG, "CTRL: Sent packet to client");
                }
                return bytes_written;
@@ -237,7 +237,7 @@ ssize_t read_pptp_header(int clientFd, unsigned char *packet, int *pptp_ctrl_typ
                                                return(0);
                                        memcpy(buffer, packet, bytes_ttl);
                                }
-                               syslog(LOG_ERR, "CTRL: Error reading ctrl packet length (bytes_ttl=%d): %s", bytes_ttl, strerror(errno));
+                               syslog(LOG_ERR, "CTRL: Error reading ctrl packet length (bytes_ttl=%lu): %s", (unsigned long) bytes_ttl, strerror(errno));
                                return -1;
                        }
                        /* FALLTHRU */
@@ -291,7 +291,7 @@ ssize_t read_pptp_header(int clientFd, unsigned char *packet, int *pptp_ctrl_typ
                                        return(0);
                                memcpy(buffer, packet, bytes_ttl);
                        }
-                       syslog(LOG_ERR, "CTRL: Error reading ctrl packet (bytes_ttl=%d,length=%d): %s", bytes_ttl, length, strerror(errno));
+                       syslog(LOG_ERR, "CTRL: Error reading ctrl packet (bytes_ttl=%lu,length=%d): %s", (unsigned long) bytes_ttl, length, strerror(errno));
                        return -1;
                }
                /* FALLTHRU */
@@ -541,7 +541,7 @@ void deal_set_link_info(unsigned char *packet)
        struct pptp_set_link_info *set_link_info;
 
        set_link_info = (struct pptp_set_link_info *) packet;
-       if (set_link_info->send_accm != 0xffffffff || set_link_info->recv_accm != 0xffffffff)
+       if (set_link_info->send_accm != 0xffffffff || set_link_info->recv_accm != 0xffffffff) {
                /* Async-Control-Character-Map (ACCM) are bits that
                   show which control characters should be escaped by the
                   PPP implementation ... pptpd leaves pppd to negotiate
@@ -550,7 +550,7 @@ void deal_set_link_info(unsigned char *packet)
                   still works. */
                if (pptpctrl_debug)
                        syslog(LOG_DEBUG, "CTRL: Ignored a SET LINK INFO packet with real ACCMs! (intentional non-compliance with section 2.15 of RFC 2637, ACCM is negotiated by PPP LCP asyncmap)");
-       else if (pptpctrl_debug)
+       } else if (pptpctrl_debug)
                syslog(LOG_DEBUG, "CTRL: Got a SET LINK INFO packet with standard ACCMs");
 }
 
diff --git a/ctrlpacket.h b/ctrlpacket.h
index d51ed2a..26449ec 100644
--- a/ctrlpacket.h
+++ b/ctrlpacket.h
@@ -10,7 +10,7 @@
 #define _PPTPD_CTRLPACKET_H
 
 int read_pptp_packet(int clientFd, unsigned char *packet, unsigned char *rply_packet, ssize_t * rply_size);
-size_t send_pptp_packet(int clientFd, unsigned char *packet, size_t packet_size);
+ssize_t send_pptp_packet(int clientFd, unsigned char *packet, size_t packet_size);
 void make_echo_req_packet(unsigned char *rply_packet, ssize_t * rply_size, u_int32_t echo_id);
 void make_call_admin_shutdown(unsigned char *rply_packet, ssize_t * rply_size);
 void make_stop_ctrl_req(unsigned char *rply_packet, ssize_t * rply_size);
diff --git a/pptpgre.c b/pptpgre.c
index 0555041..eb6dd11 100644
--- a/pptpgre.c
+++ b/pptpgre.c
@@ -140,7 +140,7 @@ int decaps_hdlc(int fd, int (*cb) (int cl, void *pack, unsigned len), int cl)
                 */
                if ((status = read(fd, buffer, sizeof(buffer))) <= 0) {
                        syslog(LOG_ERR, "GRE: read(fd=%d,buffer=%lx,len=%d) from PTY failed: status = %d error = %s%s",
-                              fd, (unsigned long) buffer, sizeof(buffer), 
+                              fd, (unsigned long) buffer, (int) sizeof(buffer), 
                               status, status ? strerror(errno) : "No error", 
                               errno != EIO ? "" : ", usually caused by unexpected termination of pppd, check option syntax and pppd logs");
                        /* FAQ: mistakes in pppd option spelling in
@@ -348,7 +348,7 @@ int decaps_gre(int fd, int (*cb) (int cl, void *pack, unsigned len), int cl)
        dequeue_gre(cb, cl);
        if ((status = read(fd, buffer, sizeof(buffer))) <= 0) {
                syslog(LOG_ERR, "GRE: read(fd=%d,buffer=%lx,len=%d) from network failed: status = %d error = %s",
-                      fd, (unsigned long) buffer, sizeof(buffer), status, status ? strerror(errno) : "No error");
+                      fd, (unsigned long) buffer, (int)sizeof(buffer), status, status ? strerror(errno) : "No error");
                stats.rx_errors++;
                return -1;
        }

------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60135031&iu=/4140/ostg.clktrk