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

Eric Blake eblake at redhat.com
Thu Apr 10 14:24:20 UTC 2014


On 04/10/2014 08:04 AM, Natanael Copa wrote:
> We must always reset errno to 0 even if we do 'continue'.
> 
> This fixes runtime with musl libc which will set errno on sscanf.
> 
> Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
> ---
>  src/nodeinfo.c | 15 +++------------
>  1 file changed, 3 insertions(+), 12 deletions(-)
> 
> diff --git a/src/nodeinfo.c b/src/nodeinfo.c
> index 53ba716..8d3214e 100644
> --- a/src/nodeinfo.c
> +++ b/src/nodeinfo.c
> @@ -452,8 +452,7 @@ virNodeParseNode(const char *node,
>  
>      /* enumerate sockets in the node */
>      CPU_ZERO(&sock_map);
> -    errno = 0;
> -    while ((cpudirent = readdir(cpudir))) {
> +    for (errno = 0; (cpudirent = readdir(cpudir)); errno = 0) {
>          if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1)
>              continue;

Good catch.  However, the code is still buggy even after your fix.  We
are missing:

if (errno)
    break;

before attempting the sscanf.  Furthermore, sscanf() is undefined on
overflow; while the cpudir is unlikely to be giving us integers that
overflow, it would be nicer to not use sscanf in the first place.  It's
more than just the improper use of readdir that needs fixing here.

I'm debating whether to push this now and fix the rest as a followup, or
whether to do a v2 that fixes it all at once.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 604 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20140410/3fe6a52d/attachment-0001.sig>


More information about the libvir-list mailing list