[libvirt] [PATCH] nodeinfo: Make sure we always reset errno before calling readdir

Natanael Copa ncopa at alpinelinux.org
Thu Apr 10 20:52:58 UTC 2014


On Thu, 10 Apr 2014 09:22:42 -0600
Eric Blake <eblake at redhat.com> wrote:

> On 04/10/2014 08:38 AM, Natanael Copa wrote:
> 
> >>> -    errno = 0;
> >>> -    while ((cpudirent = readdir(cpudir))) {
> >>> +    for (errno = 0; (cpudirent = readdir(cpudir)); errno = 0) {
> >>>          if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1)
> >>>              continue;
> >>
... 
> > 
> > I suppose we could use helper function to make it more readable:
> > 
> > static struct dirent *virReaddir(DIR *dirp)
> > {
> >     errno = 0;
> >     return readdir(dirp);
> > }
> 
> Or maybe:
> 
> int virReaddir(DIR *dirp, struct dirent **ent)
> {
>     errno = 0;
>     *ent = readdir(dirp);
>     if (!*ent && errno) {
>         virReportSystemError(errno, _("unable to read directory"))
>         return -1;
>     }
>     return *ent ? 1 : 0;
> }
> 
> and used as:
> 
> while ((ret = virReaddir(dirp, &entry)) > 0) {
>     process entry
> }
> if (ret < 0)
>     goto error;

This looks better yes.

Should I prepare a new patch with this? And grep for more readdirs?

-nc




More information about the libvir-list mailing list