Module Name:    src
Committed By:   dholland
Date:           Sun Mar 30 05:30:28 UTC 2014

Modified Files:
        src/games/hunt/hunt: connect.c hunt.c hunt_private.h playit.c
        src/games/hunt/huntd: answer.c hunt.h
        src/games/hunt/include: hunt_common.h

Log Message:
Remove SHORTLEN and LONGLEN defines; use sizeof() properly instead.
Fix two semi-compensating size bugs in wire transmission affecting
64-bit machines.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/games/hunt/hunt/connect.c
cvs rdiff -u -r1.54 -r1.55 src/games/hunt/hunt/hunt.c
cvs rdiff -u -r1.8 -r1.9 src/games/hunt/hunt/hunt_private.h
cvs rdiff -u -r1.20 -r1.21 src/games/hunt/hunt/playit.c
cvs rdiff -u -r1.21 -r1.22 src/games/hunt/huntd/answer.c
cvs rdiff -u -r1.29 -r1.30 src/games/hunt/huntd/hunt.h
cvs rdiff -u -r1.3 -r1.4 src/games/hunt/include/hunt_common.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/hunt/hunt/connect.c
diff -u src/games/hunt/hunt/connect.c:1.10 src/games/hunt/hunt/connect.c:1.11
--- src/games/hunt/hunt/connect.c:1.10	Sun Mar 30 05:14:47 2014
+++ src/games/hunt/hunt/connect.c	Sun Mar 30 05:30:28 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: connect.c,v 1.10 2014/03/30 05:14:47 dholland Exp $	*/
+/*	$NetBSD: connect.c,v 1.11 2014/03/30 05:30:28 dholland Exp $	*/
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
  * All rights reserved.
@@ -32,31 +32,33 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: connect.c,v 1.10 2014/03/30 05:14:47 dholland Exp $");
+__RCSID("$NetBSD: connect.c,v 1.11 2014/03/30 05:30:28 dholland Exp $");
 #endif /* not lint */
 
 #include <string.h>
 #include <signal.h>
 #include <unistd.h>
+#include <assert.h>
 
 #include "hunt_common.h"
 #include "hunt_private.h"
 
 void
-do_connect(char *name, char team, long enter_status)
+do_connect(const char *name, size_t namelen, char team, int enter_status)
 {
 	static int32_t uid;
-	static int32_t mode;
+	int32_t mode;
+	int32_t wire_status = htonl(enter_status);
 
 	if (uid == 0)
 		uid = htonl(getuid());
-	(void) write(huntsocket, &uid, LONGLEN);
-	(void) write(huntsocket, name, NAMELEN);
+	(void) write(huntsocket, &uid, sizeof(uid));
+	assert(namelen == WIRE_NAMELEN);
+	(void) write(huntsocket, name, namelen);
 	(void) write(huntsocket, &team, 1);
-	enter_status = htonl(enter_status);
-	(void) write(huntsocket, &enter_status, LONGLEN);
+	(void) write(huntsocket, &wire_status, sizeof(wire_status));
 	(void) strcpy(Buf, ttyname(fileno(stderr)));
-	(void) write(huntsocket, Buf, NAMELEN);
+	(void) write(huntsocket, Buf, WIRE_NAMELEN);
 #ifdef INTERNET
 	if (Send_message != NULL)
 		mode = C_MESSAGE;

Index: src/games/hunt/hunt/hunt.c
diff -u src/games/hunt/hunt/hunt.c:1.54 src/games/hunt/hunt/hunt.c:1.55
--- src/games/hunt/hunt/hunt.c:1.54	Sun Mar 30 05:14:47 2014
+++ src/games/hunt/hunt/hunt.c	Sun Mar 30 05:30:28 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: hunt.c,v 1.54 2014/03/30 05:14:47 dholland Exp $	*/
+/*	$NetBSD: hunt.c,v 1.55 2014/03/30 05:30:28 dholland Exp $	*/
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
  * All rights reserved.
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: hunt.c,v 1.54 2014/03/30 05:14:47 dholland Exp $");
+__RCSID("$NetBSD: hunt.c,v 1.55 2014/03/30 05:30:28 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -94,7 +94,7 @@ SOCKET Daemon;
 char map_key[256];			/* what to map keys to */
 bool no_beep;
 
-static char name[NAMELEN];
+static char name[WIRE_NAMELEN];
 static char team = ' ';
 
 static int in_visual;
@@ -102,7 +102,7 @@ static int in_visual;
 extern int cur_row, cur_col;
 
 static void dump_scores(const struct sockaddr_storage *, socklen_t);
-static long env_init(long);
+static int env_init(int);
 static void fill_in_blanks(void);
 static void fincurs(void);
 static void rmnl(char *);
@@ -138,16 +138,16 @@ main(int ac, char **av)
 {
 	char *term;
 	int c;
-	long enter_status;
+	int enter_status;
 	bool Query_driver = false;
 	bool Show_scores = false;
 
-	enter_status = env_init((long) Q_CLOAK);
+	enter_status = env_init(Q_CLOAK);
 	while ((c = getopt(ac, av, "Sbcfh:l:mn:op:qst:w:")) != -1) {
 		switch (c) {
 		case 'l':	/* rsh compatibility */
 		case 'n':
-			(void) strncpy(name, optarg, NAMELEN);
+			(void) strncpy(name, optarg, sizeof(name));
 			break;
 		case 't':
 			team = *optarg;
@@ -268,7 +268,7 @@ main(int ac, char **av)
 #endif
 #ifdef OTTO
 	if (Otto_mode)
-		(void) strncpy(name, "otto", NAMELEN);
+		(void) strncpy(name, "otto", sizeof(name));
 	else
 #endif
 	fill_in_blanks();
@@ -350,7 +350,7 @@ main(int ac, char **av)
 		}
 #endif
 
-		do_connect(name, team, enter_status);
+		do_connect(name, sizeof(name), team, enter_status);
 #ifdef INTERNET
 		if (Send_message != NULL) {
 			do_message();
@@ -658,8 +658,8 @@ leavex(int exitval, const char *fmt, ...
 	va_end(ap);
 }
 
-static long
-env_init(long enter_status)
+static int
+env_init(int enter_status)
 {
 	int i;
 	char *envp, *envname, *s;
@@ -690,11 +690,11 @@ env_init(long enter_status)
 				envname = s + 1;
 				if ((s = strchr(envp, ',')) == NULL) {
 					*envp = '\0';
-					strncpy(name, envname, NAMELEN);
+					strncpy(name, envname, sizeof(name));
 					break;
 				}
 				*s = '\0';
-				strncpy(name, envname, NAMELEN);
+				strncpy(name, envname, sizeof(name));
 				envp = s + 1;
 			}
 #ifdef INTERNET
@@ -759,7 +759,7 @@ env_init(long enter_status)
 		}
 		if (*envp != '\0') {
 			if (envname == NULL)
-				strncpy(name, envp, NAMELEN);
+				strncpy(name, envp, sizeof(name));
 			else
 				printf("unknown option %s\n", envp);
 		}
@@ -782,7 +782,7 @@ again:
 			putchar('\n');
 	} else {
 		printf("Enter your code name: ");
-		if (fgets(name, NAMELEN, stdin) == NULL)
+		if (fgets(name, sizeof(name), stdin) == NULL)
 			exit(1);
 	}
 	rmnl(name);

Index: src/games/hunt/hunt/hunt_private.h
diff -u src/games/hunt/hunt/hunt_private.h:1.8 src/games/hunt/hunt/hunt_private.h:1.9
--- src/games/hunt/hunt/hunt_private.h:1.8	Sun Mar 30 05:14:47 2014
+++ src/games/hunt/hunt/hunt_private.h	Sun Mar 30 05:30:28 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: hunt_private.h,v 1.8 2014/03/30 05:14:47 dholland Exp $	*/
+/*	$NetBSD: hunt_private.h,v 1.9 2014/03/30 05:30:28 dholland Exp $	*/
 
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
@@ -73,7 +73,7 @@ extern SOCKET Daemon;
  */
 
 /* in connect.c */
-void do_connect(char *, char, long);
+void do_connect(const char *, size_t, char, int32_t);
 
 /* in hunt.c */
 __dead void bad_con(void);

Index: src/games/hunt/hunt/playit.c
diff -u src/games/hunt/hunt/playit.c:1.20 src/games/hunt/hunt/playit.c:1.21
--- src/games/hunt/hunt/playit.c:1.20	Sun Mar 30 05:14:47 2014
+++ src/games/hunt/hunt/playit.c	Sun Mar 30 05:30:28 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: playit.c,v 1.20 2014/03/30 05:14:47 dholland Exp $	*/
+/*	$NetBSD: playit.c,v 1.21 2014/03/30 05:30:28 dholland Exp $	*/
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
  * All rights reserved.
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: playit.c,v 1.20 2014/03/30 05:14:47 dholland Exp $");
+__RCSID("$NetBSD: playit.c,v 1.21 2014/03/30 05:30:28 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/file.h>
@@ -93,8 +93,10 @@ playit(void)
 	int ch;
 	int y, x;
 	uint32_t version;
+	ssize_t result;
 
-	if (read(huntsocket, &version, LONGLEN) != LONGLEN) {
+	result = read(huntsocket, &version, sizeof(version));
+	if (result != (ssize_t)sizeof(version)) {
 		bad_con();
 		/* NOTREACHED */
 	}
@@ -435,8 +437,10 @@ void
 do_message(void)
 {
 	uint32_t version;
+	ssize_t result;
 
-	if (read(huntsocket, &version, LONGLEN) != LONGLEN) {
+	result = read(huntsocket, &version, sizeof(version));
+	if (result != (ssize_t)sizeof(version)) {
 		bad_con();
 		/* NOTREACHED */
 	}

Index: src/games/hunt/huntd/answer.c
diff -u src/games/hunt/huntd/answer.c:1.21 src/games/hunt/huntd/answer.c:1.22
--- src/games/hunt/huntd/answer.c:1.21	Sun Mar 30 01:44:37 2014
+++ src/games/hunt/huntd/answer.c	Sun Mar 30 05:30:28 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: answer.c,v 1.21 2014/03/30 01:44:37 dholland Exp $	*/
+/*	$NetBSD: answer.c,v 1.22 2014/03/30 05:30:28 dholland Exp $	*/
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
  * All rights reserved.
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: answer.c,v 1.21 2014/03/30 01:44:37 dholland Exp $");
+__RCSID("$NetBSD: answer.c,v 1.22 2014/03/30 05:30:28 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -48,9 +48,9 @@ __RCSID("$NetBSD: answer.c,v 1.21 2014/0
 
 #define SCOREDECAY	15
 
-static char Ttyname[NAMELEN];
+static char Ttyname[WIRE_NAMELEN];
 
-static IDENT *get_ident(uint32_t, uint32_t, char *, char);
+static IDENT *get_ident(uint32_t, uint32_t, const char *, char);
 static void stmonitor(PLAYER *);
 static void stplayer(PLAYER *, int);
 
@@ -59,10 +59,10 @@ answer(void)
 {
 	PLAYER *pp;
 	int newsock;
-	static u_long mode;
-	static char name[NAMELEN];
+	static uint32_t mode;
+	static char name[WIRE_NAMELEN];
 	static char team;
-	static int enter_status;
+	static int32_t enter_status;
 	static socklen_t socklen;
 	static uint32_t machine;
 	static uint32_t uid;
@@ -109,15 +109,15 @@ answer(void)
 	}
 
 	version = htonl((uint32_t) HUNT_VERSION);
-	(void) write(newsock, &version, LONGLEN);
-	(void) read(newsock, &uid, LONGLEN);
+	(void) write(newsock, &version, sizeof(version));
+	(void) read(newsock, &uid, sizeof(uid));
 	uid = ntohl(uid);
-	(void) read(newsock, name, NAMELEN);
+	(void) read(newsock, name, sizeof(name));
 	(void) read(newsock, &team, 1);
-	(void) read(newsock, &enter_status, LONGLEN);
-	enter_status = ntohl((unsigned long) enter_status);
-	(void) read(newsock, Ttyname, NAMELEN);
-	(void) read(newsock, &mode, sizeof mode);
+	(void) read(newsock, &enter_status, sizeof(enter_status));
+	enter_status = ntohl(enter_status);
+	(void) read(newsock, Ttyname, sizeof(Ttyname));
+	(void) read(newsock, &mode, sizeof(mode));
 	mode = ntohl(mode);
 
 	/*
@@ -398,7 +398,7 @@ rand_dir(void)
  *	Get the score structure of a player
  */
 static IDENT *
-get_ident(uint32_t machine, uint32_t uid, char *name, char team)
+get_ident(uint32_t machine, uint32_t uid, const char *name, char team)
 {
 	IDENT *ip;
 	static IDENT punt;
@@ -407,7 +407,7 @@ get_ident(uint32_t machine, uint32_t uid
 		if (ip->i_machine == machine
 		&&  ip->i_uid == uid
 		&&  ip->i_team == team
-		&&  strncmp(ip->i_name, name, NAMELEN) == 0)
+		&&  strncmp(ip->i_name, name, WIRE_NAMELEN) == 0)
 			break;
 
 	if (ip != NULL) {
@@ -427,7 +427,7 @@ get_ident(uint32_t machine, uint32_t uid
 		ip->i_machine = machine;
 		ip->i_team = team;
 		ip->i_uid = uid;
-		strncpy(ip->i_name, name, NAMELEN);
+		strncpy(ip->i_name, name, sizeof(ip->i_name));
 		ip->i_kills = 0;
 		ip->i_entries = 1;
 		ip->i_score = 0;

Index: src/games/hunt/huntd/hunt.h
diff -u src/games/hunt/huntd/hunt.h:1.29 src/games/hunt/huntd/hunt.h:1.30
--- src/games/hunt/huntd/hunt.h:1.29	Sun Mar 30 01:44:37 2014
+++ src/games/hunt/huntd/hunt.h	Sun Mar 30 05:30:28 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: hunt.h,v 1.29 2014/03/30 01:44:37 dholland Exp $	*/
+/*	$NetBSD: hunt.h,v 1.30 2014/03/30 05:30:28 dholland Exp $	*/
 
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
@@ -56,7 +56,7 @@ typedef struct ident_def	IDENT;
 typedef struct regen_def	REGEN;
 
 struct ident_def {
-	char i_name[NAMELEN];
+	char i_name[WIRE_NAMELEN];
 	char i_team;
 	uint32_t i_machine;
 	uint32_t i_uid;

Index: src/games/hunt/include/hunt_common.h
diff -u src/games/hunt/include/hunt_common.h:1.3 src/games/hunt/include/hunt_common.h:1.4
--- src/games/hunt/include/hunt_common.h:1.3	Sat Mar 29 21:55:59 2014
+++ src/games/hunt/include/hunt_common.h	Sun Mar 30 05:30:28 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: hunt_common.h,v 1.3 2014/03/29 21:55:59 dholland Exp $	*/
+/*	$NetBSD: hunt_common.h,v 1.4 2014/03/30 05:30:28 dholland Exp $	*/
 
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
@@ -85,9 +85,7 @@
 #define MAXPL		17
 #define MAXMON		0
 #endif
-#define SHORTLEN	2		/* sizeof (network short) */
-#define LONGLEN		4		/* sizeof (network long) */
-#define NAMELEN		20
+#define WIRE_NAMELEN	20
 #define MSGLEN		SCREEN_WIDTH
 #define DECAY		50.0
 

Reply via email to