[libvirt] [PATCH] tests: Do not ignore mode parameter in mocked open()

Jiri Denemark posted 1 patch 6 years, 6 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/1a354c249991187577ceb27de90cd974b230a076.1507032438.git.jdenemar@redhat.com
tests/virfilewrapper.c | 13 ++++++++++++-
tests/virusbmock.c     | 15 ++++++++++++++-
2 files changed, 26 insertions(+), 2 deletions(-)
[libvirt] [PATCH] tests: Do not ignore mode parameter in mocked open()
Posted by Jiri Denemark 6 years, 6 months ago
From: Luyao Huang <lhuang@redhat.com>

This is normally not an issue since the tests which use mocked open() do
not create files. But once coverage build is enabled, gcov_open will use
O_CREATE and real_open will read random data rather than the actual mode
argument.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 tests/virfilewrapper.c | 13 ++++++++++++-
 tests/virusbmock.c     | 15 ++++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/tests/virfilewrapper.c b/tests/virfilewrapper.c
index fede7b2e89..1d1d182708 100644
--- a/tests/virfilewrapper.c
+++ b/tests/virfilewrapper.c
@@ -257,10 +257,21 @@ int open(const char *path, int flags, ...)
 {
     int ret = -1;
     char *newpath = NULL;
+    va_list ap;
+    mode_t mode = 0;
 
     PATH_OVERRIDE(newpath, path);
 
-    ret = real_open(newpath, flags);
+    /* The mode argument is mandatory when O_CREAT is set in flags,
+     * otherwise the argument is ignored.
+     */
+    if (flags & O_CREAT) {
+        va_start(ap, flags);
+        mode = va_arg(ap, mode_t);
+        va_end(ap);
+    }
+
+    ret = real_open(newpath, flags, mode);
 
     VIR_FREE(newpath);
 
diff --git a/tests/virusbmock.c b/tests/virusbmock.c
index 8d60664944..f430a2edad 100644
--- a/tests/virusbmock.c
+++ b/tests/virusbmock.c
@@ -87,13 +87,26 @@ int open(const char *pathname, int flags, ...)
 {
     char *path;
     int ret;
+    va_list ap;
+    mode_t mode = 0;
 
     init_syms();
 
     path = get_fake_path(pathname);
     if (!path)
         return -1;
-    ret = realopen(path, flags);
+
+    /* The mode argument is mandatory when O_CREAT is set in flags,
+     * otherwise the argument is ignored.
+     */
+    if (flags & O_CREAT) {
+        va_start(ap, flags);
+        mode = va_arg(ap, mode_t);
+        va_end(ap);
+    }
+
+    ret = realopen(path, flags, mode);
+
     VIR_FREE(path);
     return ret;
 }
-- 
2.14.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] tests: Do not ignore mode parameter in mocked open()
Posted by Martin Kletzander 6 years, 6 months ago
On Tue, Oct 03, 2017 at 02:07:18PM +0200, Jiri Denemark wrote:
>From: Luyao Huang <lhuang@redhat.com>
>
>This is normally not an issue since the tests which use mocked open() do
>not create files. But once coverage build is enabled, gcov_open will use
>O_CREATE and real_open will read random data rather than the actual mode
>argument.
>
>Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
>---
> tests/virfilewrapper.c | 13 ++++++++++++-
> tests/virusbmock.c     | 15 ++++++++++++++-
> 2 files changed, 26 insertions(+), 2 deletions(-)
>

ACK, I guess you don't need this in the release, do you?
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] tests: Do not ignore mode parameter in mocked open()
Posted by Daniel P. Berrange 6 years, 6 months ago
On Tue, Oct 03, 2017 at 02:07:18PM +0200, Jiri Denemark wrote:
> From: Luyao Huang <lhuang@redhat.com>
> 
> This is normally not an issue since the tests which use mocked open() do
> not create files. But once coverage build is enabled, gcov_open will use
> O_CREATE and real_open will read random data rather than the actual mode
> argument.
> 
> Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
> ---
>  tests/virfilewrapper.c | 13 ++++++++++++-
>  tests/virusbmock.c     | 15 ++++++++++++++-
>  2 files changed, 26 insertions(+), 2 deletions(-)
> 
> diff --git a/tests/virfilewrapper.c b/tests/virfilewrapper.c
> index fede7b2e89..1d1d182708 100644
> --- a/tests/virfilewrapper.c
> +++ b/tests/virfilewrapper.c
> @@ -257,10 +257,21 @@ int open(const char *path, int flags, ...)
>  {
>      int ret = -1;
>      char *newpath = NULL;
> +    va_list ap;
> +    mode_t mode = 0;
>  
>      PATH_OVERRIDE(newpath, path);
>  
> -    ret = real_open(newpath, flags);
> +    /* The mode argument is mandatory when O_CREAT is set in flags,
> +     * otherwise the argument is ignored.
> +     */
> +    if (flags & O_CREAT) {
> +        va_start(ap, flags);
> +        mode = va_arg(ap, mode_t);
> +        va_end(ap);
> +    }

This breaks the build with clang due to use of mode_t type

virfilewrapper.c:270:27: error: second argument to 'va_arg' is of promotable type 'mode_t' (aka 'unsigned short'); this va_arg has undefined behavior because arguments will be promoted to 'int' [-Werror,-Wvarargs]

        mode = va_arg(ap, mode_t);

                          ^~~~~~

I guess we should use 'int' as the arg to va_arg(), and then cast the
result back to mode_t ?


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list