[PATCH] safe_gethostname

Denys Vlasenko vda.linux at googlemail.com
Mon Feb 25 15:48:57 UTC 2008


On Monday 25 February 2008 00:46, Tito wrote:
> Hi,
> this is a preliminary patch that adds the new libbb function
> safe_gethostname  and converts all
> occurrences of gethostname to it.

+char *safe_gethostname(void)
+{
+       struct utsname uts;
+       /* The length of the arrays in a struct utsname is unspecified;*/
+       /* the fields are terminated by a null byte (Б..\0Б..). */
                                                     ^^^^^^^^

Your patch has Unicode (UTF-8) symbols for " (quote).

+       return xstrndup((uname(&uts) < 0 || !*(uts.nodename)) ? "?" : uts.nodename, 256);
+}

Why 256?

If fields are indeed terminated by NUL, just xstrdup(uts.nodename)
will work.

Otherwise (if you fear that they may not be),
xstrndup(uts.nodename, sizeof(uts.nodename));


Checking for uname() failure is not needed. It never fails.
From kernel:

asmlinkage long sys_uname(struct new_utsname __user * name)
{
        int err;
        down_read(&uts_sem);
        err = copy_to_user(name, utsname(), sizeof (*name));
        up_read(&uts_sem);
        if (personality(current->personality) == PER_LINUX32)
                err |= copy_to_user(&name->machine, "i686", 5);
        return err ? -EFAULT : 0;
}

It can fail only if you will pass bad pointer to it,
and you surely are not doing that, right?
--
vda



More information about the busybox mailing list