[libvirt] [PATCH] storage_backend.c: avoid closing a negative file descriptor

This close(fd) is reachable with an "fd" of -1 via the "goto cleanup" just before &fd is first set. While closing(-1) is not a big problem, it is a failing syscall, and would show up on an strace audit, not to mention the coverity and maybe-clang warnings.
From c69369c445be53f12ec09a176fd477b9ff16bbff Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Tue, 2 Feb 2010 11:11:49 +0100 Subject: [PATCH] storage_backend.c: avoid closing a negative file descriptor
* src/storage/storage_backend.c (virStorageBackendRunProgRegex): Don't close a negative (read-only) file descriptor. --- src/storage/storage_backend.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index bc656f2..84eb8aa 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -1,7 +1,7 @@ /* * storage_backend.c: internal storage driver backend contract * - * Copyright (C) 2007-2009 Red Hat, Inc. + * Copyright (C) 2007-2010 Red Hat, Inc. * Copyright (C) 2007-2008 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -1326,8 +1326,10 @@ virStorageBackendRunProgRegex(virConnectPtr conn, if (list) fclose(list); - else - close(fd); + else { + if (0 <= fd) + close(fd); + } while ((err = waitpid(child, &exitstatus, 0) == -1) && errno == EINTR); -- 1.7.0.rc1.149.g0b0b7

On Tue, Feb 02, 2010 at 11:14:39AM +0100, Jim Meyering wrote:
This close(fd) is reachable with an "fd" of -1 via the "goto cleanup" just before &fd is first set. While closing(-1) is not a big problem, it is a failing syscall, and would show up on an strace audit, not to mention the coverity and maybe-clang warnings.
From c69369c445be53f12ec09a176fd477b9ff16bbff Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Tue, 2 Feb 2010 11:11:49 +0100 Subject: [PATCH] storage_backend.c: avoid closing a negative file descriptor
* src/storage/storage_backend.c (virStorageBackendRunProgRegex): Don't close a negative (read-only) file descriptor. --- src/storage/storage_backend.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index bc656f2..84eb8aa 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -1,7 +1,7 @@ /* * storage_backend.c: internal storage driver backend contract * - * Copyright (C) 2007-2009 Red Hat, Inc. + * Copyright (C) 2007-2010 Red Hat, Inc. * Copyright (C) 2007-2008 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -1326,8 +1326,10 @@ virStorageBackendRunProgRegex(virConnectPtr conn,
if (list) fclose(list); - else - close(fd); + else { + if (0 <= fd) + close(fd); + }
Can you put conditionals the normal way around with variable being tested coming first. Regards, Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

Daniel P. Berrange wrote:
On Tue, Feb 02, 2010 at 11:14:39AM +0100, Jim Meyering wrote:
This close(fd) is reachable with an "fd" of -1 via the "goto cleanup" just before &fd is first set. While closing(-1) is not a big problem, it is a failing syscall, and would show up on an strace audit, not to mention the coverity and maybe-clang warnings.
From c69369c445be53f12ec09a176fd477b9ff16bbff Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Tue, 2 Feb 2010 11:11:49 +0100 Subject: [PATCH] storage_backend.c: avoid closing a negative file descriptor
* src/storage/storage_backend.c (virStorageBackendRunProgRegex): Don't close a negative (read-only) file descriptor. --- src/storage/storage_backend.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index bc656f2..84eb8aa 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -1,7 +1,7 @@ /* * storage_backend.c: internal storage driver backend contract * - * Copyright (C) 2007-2009 Red Hat, Inc. + * Copyright (C) 2007-2010 Red Hat, Inc. * Copyright (C) 2007-2008 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -1326,8 +1326,10 @@ virStorageBackendRunProgRegex(virConnectPtr conn,
if (list) fclose(list); - else - close(fd); + else { + if (0 <= fd) + close(fd); + }
Can you put conditionals the normal way around with variable being tested coming first.
Yeah. Forgot. "Normal" is context sensitive. In many other places where I contribute, "normal" means "<" and "<=", but no (or very few) uses of ">" or ">=". Definitely an acquired habit.

On Tue, Feb 02, 2010 at 11:14:39AM +0100, Jim Meyering wrote:
This close(fd) is reachable with an "fd" of -1 via the "goto cleanup" just before &fd is first set. While closing(-1) is not a big problem, it is a failing syscall, and would show up on an strace audit, not to mention the coverity and maybe-clang warnings.
From c69369c445be53f12ec09a176fd477b9ff16bbff Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyering@redhat.com> Date: Tue, 2 Feb 2010 11:11:49 +0100 Subject: [PATCH] storage_backend.c: avoid closing a negative file descriptor
* src/storage/storage_backend.c (virStorageBackendRunProgRegex): Don't close a negative (read-only) file descriptor. --- src/storage/storage_backend.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index bc656f2..84eb8aa 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -1,7 +1,7 @@ /* * storage_backend.c: internal storage driver backend contract * - * Copyright (C) 2007-2009 Red Hat, Inc. + * Copyright (C) 2007-2010 Red Hat, Inc. * Copyright (C) 2007-2008 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -1326,8 +1326,10 @@ virStorageBackendRunProgRegex(virConnectPtr conn,
if (list) fclose(list); - else - close(fd); + else { + if (0 <= fd) + close(fd); + }
while ((err = waitpid(child, &exitstatus, 0) == -1) && errno == EINTR);
ACK, tangential, though I know tests like (0 <= fd) are safer than (fd >= 0) from a coding and maintainance perspective, I still find that harder to read and understand. Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ daniel@veillard.com | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/
participants (3)
-
Daniel P. Berrange
-
Daniel Veillard
-
Jim Meyering