[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: battlestar strdup()



On Fri, 26 Sep 2003, Marc Balmer wrote:

> > (In getutmp())
> > 89: 		return(strdup(ptr->pw_name));
> > 
> > And it doesn't really seem to be checked:
> 
> Here is a patch for this:
> 
> --- init.c.orig Fri Sep 26 19:30:37 2003
> +++ init.c      Fri Sep 26 19:31:33 2003
> @@ -85,8 +85,11 @@
>         ptr = getpwuid(getuid());
>         if (ptr == NULL)
>                 return("");
> -       else
> -               return(strdup(ptr->pw_name));
> +       else {
> +               char *nm;
> +               nm = strdup(ptr->pw_name);
> +               return nm != NULL ? nm : "";
> +       }

probably a bad idea to be returning empty strings.  otherwise, you don't 
get a name.  better fix.

Index: init.c
===================================================================
RCS file: /cvs/src/games/battlestar/init.c,v
retrieving revision 1.9
diff -u -r1.9 init.c
--- init.c	2002/12/06 21:48:51	1.9
+++ init.c	2003/09/26 18:45:05
@@ -61,6 +61,8 @@
 	location = dayfile;
 	srandomdev();
 	username = getutmp();
+	if (username == NULL)
+		errx(1, "Don't know who you are.");
 	wordinit();
 	if (filename == NULL) {
 		direction = NORTH;
@@ -88,7 +90,7 @@
 
 	ptr = getpwuid(getuid());
 	if (ptr == NULL)
-		return("");
+		return(NULL);
 	else
 		return(strdup(ptr->pw_name));
 }


-- 
ask not what you can do for your country
ask what your country did to you