[SLOF] [PATCH] libnet: Get rid of unnecessary (char *) casts

Thomas Huth thuth at redhat.com
Wed May 9 21:12:40 AEST 2018


For some strange reasons, the libnet code is using int8_t arrays for
strings in a couple of places where it really does not make any sense.
Therefor a lot of "(char *)" casts are needed when the code is using
the string functions from the libc. Let's change the strings to use
"char" instead of "int8_t" so we can get rid of a lot of these casts.

Signed-off-by: Thomas Huth <thuth at redhat.com>
---
 lib/libnet/tftp.h    |  2 +-
 lib/libnet/bootp.c   |  2 +-
 lib/libnet/dhcp.c    | 43 +++++++++++++++++++++++--------------------
 lib/libnet/dhcpv6.c  |  3 +--
 lib/libnet/netload.c |  2 +-
 lib/libnet/tftp.c    | 16 ++++++++--------
 6 files changed, 35 insertions(+), 33 deletions(-)

diff --git a/lib/libnet/tftp.h b/lib/libnet/tftp.h
index b1dbc21..e32e473 100644
--- a/lib/libnet/tftp.h
+++ b/lib/libnet/tftp.h
@@ -28,7 +28,7 @@ struct filename_ip {
 	uint32_t server_ip;
 	ip6_addr_t server_ip6;
 	ip6_addr_t dns_ip6;
-	int8_t filename[256];
+	char filename[256];
 	int    fd;
 } __attribute__ ((packed));
 typedef struct filename_ip filename_ip_t;
diff --git a/lib/libnet/bootp.c b/lib/libnet/bootp.c
index 6d58cef..464cb66 100644
--- a/lib/libnet/bootp.c
+++ b/lib/libnet/bootp.c
@@ -166,7 +166,7 @@ receive_bootp(filename_ip_t * fn_ip)
 
 		fn_ip->own_ip = btph->yiaddr;
 		fn_ip->server_ip = btph->siaddr;
-		strcpy((char *) fn_ip->filename, (char *) btph->file);
+		strcpy(fn_ip->filename, (char *)btph->file);
 
 #if DEBUG
 		printf("\nThese are the details of the bootp reply:\n");
diff --git a/lib/libnet/dhcp.c b/lib/libnet/dhcp.c
index 0cb4fa4..d3e5170 100644
--- a/lib/libnet/dhcp.c
+++ b/lib/libnet/dhcp.c
@@ -124,8 +124,8 @@ typedef struct {
 	uint32_t   subnet_mask;       /**< o. 1 Subnet mask                    */
 	uint8_t    msg_type;          /**< o.53 DHCP-message type              */
 	uint8_t    overload;          /**< o.52 Overload sname/file fields     */
-	int8_t     tftp_server[256];  /**< o.66 TFTP server name               */
-	int8_t     bootfile[256];     /**< o.67 Boot file name                 */
+	char       tftp_server[256];  /**< o.66 TFTP server name               */
+	char       bootfile[256];     /**< o.67 Boot file name                 */
 	uint16_t   client_arch;       /**< o.93 Client architecture type       */
 } dhcp_options_t;
 
@@ -197,7 +197,7 @@ int32_t dhcpv4(char *ret_buffer, filename_ip_t *fn_ip)
 		dhcp_siaddr_ip = fn_ip->server_ip;
 	}
 	if(fn_ip->filename[0] != 0) {
-		strcpy(dhcp_filename, (char *) fn_ip->filename);
+		strcpy(dhcp_filename, fn_ip->filename);
 	}
 
 	// TFTP SERVER
@@ -230,7 +230,7 @@ int32_t dhcpv4(char *ret_buffer, filename_ip_t *fn_ip)
 	// Store configuration info into filename_ip strucutre
 	fn_ip -> own_ip = dhcp_own_ip;
 	fn_ip -> server_ip = dhcp_tftp_ip;
-	strcpy((char *) fn_ip -> filename, dhcp_filename);
+	strcpy(fn_ip->filename, dhcp_filename);
 
 	return 0;
 }
@@ -342,14 +342,14 @@ static int32_t dhcp_encode_options(uint8_t * opt_field, dhcp_options_t * opt_str
 
 	if (opt_struct -> flag[DHCP_TFTP_SERVER]) {
 		options[0] = DHCP_TFTP_SERVER;
-		options[1] = strlen((char *) opt_struct -> tftp_server) + 1;
+		options[1] = strlen(opt_struct->tftp_server) + 1;
 		memcpy(options + 2, opt_struct -> tftp_server, options[1]);
 		options += options[1] + 2;
 	}
 
 	if (opt_struct -> flag[DHCP_BOOTFILE]) {
 		options[0] = DHCP_BOOTFILE;
-		options[1] = strlen((char *) opt_struct -> bootfile) + 1;
+		options[1] = strlen(opt_struct->bootfile) + 1;
 		memcpy(options + 2, opt_struct -> bootfile, options[1]);
 		options += options[1] + 2;
 	}
@@ -716,7 +716,7 @@ void dhcp_send_release(int fd)
 	btph -> htype = 1;
 	btph -> hlen = 6;
 	btph -> xid = dhcp_xid;
-	strcpy((char *) btph -> file, "");
+	btph -> file[0] = 0;
 	memcpy(btph -> chaddr, get_mac_address(), 6);
 	btph -> ciaddr = htonl(dhcp_own_ip);
 
@@ -774,13 +774,14 @@ int8_t handle_dhcp(int fd, uint8_t * packet, int32_t packetsize)
 		dhcp_server_ip = htonl(iph -> ip_src);
 
 		if (strlen((char *) btph -> sname) && !dhcp_siaddr_ip) {
-			strncpy((char *) dhcp_tftp_name, (char *) btph -> sname,
-			        sizeof(btph -> sname));
+			strncpy(dhcp_tftp_name, (char *)btph->sname,
+			        sizeof(btph->sname));
 			dhcp_tftp_name[sizeof(btph -> sname)] = 0;
 		}
 
 		if (strlen((char *) btph -> file)) {
-			strncpy((char *) dhcp_filename, (char *) btph -> file, sizeof(btph -> file));
+			strncpy(dhcp_filename, (char *)btph->file,
+			        sizeof(btph->file));
 			dhcp_filename[sizeof(btph -> file)] = 0;
 		}
 
@@ -845,12 +846,14 @@ int8_t handle_dhcp(int fd, uint8_t * packet, int32_t packetsize)
 		dhcp_own_ip = htonl(btph -> yiaddr);
 		dhcp_siaddr_ip = htonl(btph -> siaddr);
 		if (strlen((char *) btph -> sname) && !dhcp_siaddr_ip) {
-			strncpy((char *) dhcp_tftp_name, (char *) btph -> sname, sizeof(btph -> sname));
+			strncpy(dhcp_tftp_name, (char *)btph->sname,
+			        sizeof(btph->sname));
 			dhcp_tftp_name[sizeof(btph -> sname)] = 0;
 		}
 
 		if (strlen((char *) btph -> file)) {
-			strncpy((char *) dhcp_filename, (char *) btph -> file, sizeof(btph -> file));
+			strncpy(dhcp_filename, (char *)btph->file,
+			        sizeof(btph->file));
 			dhcp_filename[sizeof(btph -> file)] = 0;
 		}
 
@@ -883,14 +886,14 @@ int8_t handle_dhcp(int fd, uint8_t * packet, int32_t packetsize)
 				dhcp_server_ip = opt.server_ID;
 				dhcp_siaddr_ip = htonl(btph -> siaddr);
 				if (opt.flag[DHCP_TFTP_SERVER]) {
-					strcpy((char *) dhcp_tftp_name, (char *) opt.tftp_server);
+					strcpy(dhcp_tftp_name, opt.tftp_server);
 				}
 				else {
-					strcpy((char *) dhcp_tftp_name, "");
+					dhcp_filename[0] = 0;
 					if ((opt.overload != DHCP_OVERLOAD_SNAME &&
 					     opt.overload != DHCP_OVERLOAD_BOTH) &&
 					     !dhcp_siaddr_ip) {
-						strncpy((char *) dhcp_tftp_name,
+						strncpy(dhcp_tftp_name,
 						        (char *) btph->sname,
 						        sizeof(btph -> sname));
 						dhcp_tftp_name[sizeof(btph->sname)] = 0;
@@ -898,14 +901,14 @@ int8_t handle_dhcp(int fd, uint8_t * packet, int32_t packetsize)
 				}
 
 				if (opt.flag[DHCP_BOOTFILE]) {
-					strcpy((char *) dhcp_filename, (char *) opt.bootfile);
+					strcpy(dhcp_filename, opt.bootfile);
 				}
 				else {
-					strcpy((char *) dhcp_filename, "");
+					dhcp_filename[0] = 0;
 					if (opt.overload != DHCP_OVERLOAD_FILE &&
-						opt.overload != DHCP_OVERLOAD_BOTH &&
-						strlen((char *) btph -> file)) {
-						strncpy((char *) dhcp_filename,
+					    opt.overload != DHCP_OVERLOAD_BOTH &&
+					    strlen((char *)btph->file)) {
+						strncpy(dhcp_filename,
 						        (char *) btph->file,
 						        sizeof(btph->file));
 						dhcp_filename[sizeof(btph -> file)] = 0;
diff --git a/lib/libnet/dhcpv6.c b/lib/libnet/dhcpv6.c
index 491d540..e92fc0f 100644
--- a/lib/libnet/dhcpv6.c
+++ b/lib/libnet/dhcpv6.c
@@ -176,8 +176,7 @@ static void dhcp6_process_options (uint8_t *option, int32_t option_length)
 			buffer[option_boot_url->length] = 0;
 			if (parse_tftp_args(buffer,
 					    (char *)my_fn_ip->server_ip6.addr,
-					    (char *)my_fn_ip->filename,
-					    (int)my_fn_ip->fd,
+					    my_fn_ip->filename, my_fn_ip->fd,
 					    option_boot_url->length) == -1)
 				return;
 			break;
diff --git a/lib/libnet/netload.c b/lib/libnet/netload.c
index 74e3464..5c37fe2 100644
--- a/lib/libnet/netload.c
+++ b/lib/libnet/netload.c
@@ -735,7 +735,7 @@ int netload(char *buffer, int len, int huge_load, int block_size,
 	 ***********************************************************/
 
 	if (obp_tftp_args.filename[0] != 0) {
-		strncpy((char *) fn_ip.filename, obp_tftp_args.filename, sizeof(fn_ip.filename)-1);
+		strncpy(fn_ip.filename, obp_tftp_args.filename, sizeof(fn_ip.filename)-1);
 		fn_ip.filename[sizeof(fn_ip.filename)-1] = 0;
 	}
 
diff --git a/lib/libnet/tftp.c b/lib/libnet/tftp.c
index cda8bf3..1656c27 100644
--- a/lib/libnet/tftp.c
+++ b/lib/libnet/tftp.c
@@ -97,7 +97,7 @@ static void send_rrq(int fd)
 	int ip_len = 0;
 	int ip6_payload_len    = 0;
 	unsigned short udp_len = 0;
-	unsigned char mode[] = "octet";
+	const char mode[] = "octet";
 	char *ptr	     = NULL;
 	struct iphdr *ip     = NULL;
 	struct ip6hdr *ip6   = NULL;
@@ -110,7 +110,7 @@ static void send_rrq(int fd)
 		ip = (struct iphdr *) packet;
 		udph = (struct udphdr *) (ip + 1);
 		ip_len = sizeof(struct iphdr) + sizeof(struct udphdr)
-			+ strlen((char *) fn_ip->filename) + strlen((char *) mode) + 4
+			+ strlen(fn_ip->filename) + strlen(mode) + 4
 			+ strlen("blksize") + strlen(blocksize_str) + 2;
 		fill_iphdr ((uint8_t *) ip, ip_len, IPTYPE_UDP, 0,
 			    fn_ip->server_ip);
@@ -119,7 +119,7 @@ static void send_rrq(int fd)
 		ip6 = (struct ip6hdr *) packet;
 		udph = (struct udphdr *) (ip6 + 1);
 		ip6_payload_len = sizeof(struct udphdr)
-			+ strlen((char *) fn_ip->filename) + strlen((char *) mode) + 4
+			+ strlen(fn_ip->filename) + strlen(mode) + 4
 			+ strlen("blksize") + strlen(blocksize_str) + 2;
 		ip_len = sizeof(struct ip6hdr) + ip6_payload_len;
 		fill_ip6hdr ((uint8_t *) ip6, ip6_payload_len, IPTYPE_UDP, get_ipv6_address(),
@@ -127,7 +127,7 @@ static void send_rrq(int fd)
 
 	}
 	udp_len = htons(sizeof(struct udphdr)
-			      + strlen((char *) fn_ip->filename) + strlen((char *) mode) + 4
+			      + strlen(fn_ip->filename) + strlen(mode) + 4
 			      + strlen("blksize") + strlen(blocksize_str) + 2);
 	fill_udphdr ((uint8_t *) udph, udp_len, htons(2001), htons(69));
 
@@ -135,12 +135,12 @@ static void send_rrq(int fd)
 	tftp->th_opcode = htons(RRQ);
 
 	ptr = (char *) &tftp->th_data;
-	memcpy(ptr, fn_ip->filename, strlen((char *) fn_ip->filename) + 1);
+	memcpy(ptr, fn_ip->filename, strlen(fn_ip->filename) + 1);
 
-	ptr += strlen((char *) fn_ip->filename) + 1;
-	memcpy(ptr, mode, strlen((char *) mode) + 1);
+	ptr += strlen(fn_ip->filename) + 1;
+	memcpy(ptr, mode, strlen(mode) + 1);
 
-	ptr += strlen((char *) mode) + 1;
+	ptr += strlen(mode) + 1;
 	memcpy(ptr, "blksize", strlen("blksize") + 1);
 
 	ptr += strlen("blksize") + 1;
-- 
1.8.3.1



More information about the SLOF mailing list