diff mbox

[4/4] 9pfs: introduce v9fs_path_sprintf() helper

Message ID 147394946678.684.4033631335977230764.stgit@bahia (mailing list archive)
State New, archived
Headers show

Commit Message

Greg Kurz Sept. 15, 2016, 2:24 p.m. UTC
This helper is similar to v9fs_string_sprintf(), but it includes the
terminating NUL character in the size field.

This is to avoid doing v9fs_string_sprintf((V9fsString *) &path) and
then bumping the size.

Affected users are changed to use this new helper.

Signed-off-by: Greg Kurz <groug@kaod.org>
---
 hw/9pfs/9p-local.c |    7 ++-----
 hw/9pfs/9p-proxy.c |    7 ++-----
 hw/9pfs/9p.c       |   19 ++++++++++++++++---
 hw/9pfs/9p.h       |    1 +
 4 files changed, 21 insertions(+), 13 deletions(-)

Comments

Cédric Le Goater Sept. 15, 2016, 3:09 p.m. UTC | #1
On 09/15/2016 04:24 PM, Greg Kurz wrote:
> This helper is similar to v9fs_string_sprintf(), but it includes the
> terminating NUL character in the size field.

NULL

> This is to avoid doing v9fs_string_sprintf((V9fsString *) &path) and
> then bumping the size.
> 
> Affected users are changed to use this new helper.



Reviewed-by: Cédric Le Goater <clg@kaod.org>

 
> Signed-off-by: Greg Kurz <groug@kaod.org>
> ---
>  hw/9pfs/9p-local.c |    7 ++-----
>  hw/9pfs/9p-proxy.c |    7 ++-----
>  hw/9pfs/9p.c       |   19 ++++++++++++++++---
>  hw/9pfs/9p.h       |    1 +
>  4 files changed, 21 insertions(+), 13 deletions(-)
> 
> diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
> index 3f271fcbd2c5..845675e7a1bb 100644
> --- a/hw/9pfs/9p-local.c
> +++ b/hw/9pfs/9p-local.c
> @@ -1058,17 +1058,14 @@ static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
>  
>  static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
>                                const char *name, V9fsPath *target)
>  {
>      if (dir_path) {
> -        v9fs_string_sprintf((V9fsString *)target, "%s/%s",
> -                            dir_path->data, name);
> +        v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
>      } else {
> -        v9fs_string_sprintf((V9fsString *)target, "%s", name);
> +        v9fs_path_sprintf(target, "%s", name);
>      }
> -    /* Bump the size for including terminating NULL */
> -    target->size++;
>      return 0;
>  }
>  
>  static int local_renameat(FsContext *ctx, V9fsPath *olddir,
>                            const char *old_name, V9fsPath *newdir,
> diff --git a/hw/9pfs/9p-proxy.c b/hw/9pfs/9p-proxy.c
> index d091564b6fd2..f2417b7fd73d 100644
> --- a/hw/9pfs/9p-proxy.c
> +++ b/hw/9pfs/9p-proxy.c
> @@ -1021,17 +1021,14 @@ static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
>  
>  static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
>                                const char *name, V9fsPath *target)
>  {
>      if (dir_path) {
> -        v9fs_string_sprintf((V9fsString *)target, "%s/%s",
> -                            dir_path->data, name);
> +        v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
>      } else {
> -        v9fs_string_sprintf((V9fsString *)target, "%s", name);
> +        v9fs_path_sprintf(target, "%s", name);
>      }
> -    /* Bump the size for including terminating NULL */
> -    target->size++;
>      return 0;
>  }
>  
>  static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
>                            const char *old_name, V9fsPath *newdir,
> diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c
> index d8f48ca76c47..639f93930285 100644
> --- a/hw/9pfs/9p.c
> +++ b/hw/9pfs/9p.c
> @@ -10,10 +10,11 @@
>   * the COPYING file in the top-level directory.
>   *
>   */
>  
>  #include "qemu/osdep.h"
> +#include <glib/gprintf.h>
>  #include "hw/virtio/virtio.h"
>  #include "qapi/error.h"
>  #include "qemu/error-report.h"
>  #include "qemu/iov.h"
>  #include "qemu/sockets.h"
> @@ -177,10 +178,24 @@ void v9fs_path_free(V9fsPath *path)
>      g_free(path->data);
>      path->data = NULL;
>      path->size = 0;
>  }
>  
> +
> +void GCC_FMT_ATTR(2, 3)
> +v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
> +{
> +    va_list ap;
> +
> +    v9fs_path_free(path);
> +
> +    va_start(ap, fmt);
> +    /* Bump the size for including terminating NULL */
> +    path->size = g_vasprintf(&path->data, fmt, ap) + 1;
> +    va_end(ap);
> +}
> +
>  void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
>  {
>      v9fs_path_free(lhs);
>      lhs->data = g_malloc(rhs->size);
>      memcpy(lhs->data, rhs->data, rhs->size);
> @@ -915,14 +930,12 @@ static void print_sg(struct iovec *sg, int cnt)
>  static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
>  {
>      V9fsPath str;
>      v9fs_path_init(&str);
>      v9fs_path_copy(&str, dst);
> -    v9fs_string_sprintf((V9fsString *)dst, "%s%s", src->data, str.data+len);
> +    v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
>      v9fs_path_free(&str);
> -    /* +1 to include terminating NULL */
> -    dst->size++;
>  }
>  
>  static inline bool is_ro_export(FsContext *ctx)
>  {
>      return ctx->export_flags & V9FS_RDONLY;
> diff --git a/hw/9pfs/9p.h b/hw/9pfs/9p.h
> index a38603398ef5..d539d2ebe9c0 100644
> --- a/hw/9pfs/9p.h
> +++ b/hw/9pfs/9p.h
> @@ -325,10 +325,11 @@ static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
>  }
>  
>  extern void v9fs_reclaim_fd(V9fsPDU *pdu);
>  extern void v9fs_path_init(V9fsPath *path);
>  extern void v9fs_path_free(V9fsPath *path);
> +extern void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
>  extern void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs);
>  extern int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
>                               const char *name, V9fsPath *path);
>  extern int v9fs_device_realize_common(V9fsState *s, Error **errp);
>  extern void v9fs_device_unrealize_common(V9fsState *s, Error **errp);
> 
>
Greg Kurz Sept. 15, 2016, 3:24 p.m. UTC | #2
On Thu, 15 Sep 2016 17:09:08 +0200
Cédric Le Goater <clg@kaod.org> wrote:

> On 09/15/2016 04:24 PM, Greg Kurz wrote:
> > This helper is similar to v9fs_string_sprintf(), but it includes the
> > terminating NUL character in the size field.  
> 
> NULL
> 

The null character is often abbreviated NUL.

https://en.wikipedia.org/wiki/Null_character

> > This is to avoid doing v9fs_string_sprintf((V9fsString *) &path) and
> > then bumping the size.
> > 
> > Affected users are changed to use this new helper.  
> 
> 
> 
> Reviewed-by: Cédric Le Goater <clg@kaod.org>
> 
>  
> > Signed-off-by: Greg Kurz <groug@kaod.org>
> > ---
> >  hw/9pfs/9p-local.c |    7 ++-----
> >  hw/9pfs/9p-proxy.c |    7 ++-----
> >  hw/9pfs/9p.c       |   19 ++++++++++++++++---
> >  hw/9pfs/9p.h       |    1 +
> >  4 files changed, 21 insertions(+), 13 deletions(-)
> > 
> > diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
> > index 3f271fcbd2c5..845675e7a1bb 100644
> > --- a/hw/9pfs/9p-local.c
> > +++ b/hw/9pfs/9p-local.c
> > @@ -1058,17 +1058,14 @@ static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
> >  
> >  static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
> >                                const char *name, V9fsPath *target)
> >  {
> >      if (dir_path) {
> > -        v9fs_string_sprintf((V9fsString *)target, "%s/%s",
> > -                            dir_path->data, name);
> > +        v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
> >      } else {
> > -        v9fs_string_sprintf((V9fsString *)target, "%s", name);
> > +        v9fs_path_sprintf(target, "%s", name);
> >      }
> > -    /* Bump the size for including terminating NULL */
> > -    target->size++;
> >      return 0;
> >  }
> >  
> >  static int local_renameat(FsContext *ctx, V9fsPath *olddir,
> >                            const char *old_name, V9fsPath *newdir,
> > diff --git a/hw/9pfs/9p-proxy.c b/hw/9pfs/9p-proxy.c
> > index d091564b6fd2..f2417b7fd73d 100644
> > --- a/hw/9pfs/9p-proxy.c
> > +++ b/hw/9pfs/9p-proxy.c
> > @@ -1021,17 +1021,14 @@ static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
> >  
> >  static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
> >                                const char *name, V9fsPath *target)
> >  {
> >      if (dir_path) {
> > -        v9fs_string_sprintf((V9fsString *)target, "%s/%s",
> > -                            dir_path->data, name);
> > +        v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
> >      } else {
> > -        v9fs_string_sprintf((V9fsString *)target, "%s", name);
> > +        v9fs_path_sprintf(target, "%s", name);
> >      }
> > -    /* Bump the size for including terminating NULL */
> > -    target->size++;
> >      return 0;
> >  }
> >  
> >  static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
> >                            const char *old_name, V9fsPath *newdir,
> > diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c
> > index d8f48ca76c47..639f93930285 100644
> > --- a/hw/9pfs/9p.c
> > +++ b/hw/9pfs/9p.c
> > @@ -10,10 +10,11 @@
> >   * the COPYING file in the top-level directory.
> >   *
> >   */
> >  
> >  #include "qemu/osdep.h"
> > +#include <glib/gprintf.h>
> >  #include "hw/virtio/virtio.h"
> >  #include "qapi/error.h"
> >  #include "qemu/error-report.h"
> >  #include "qemu/iov.h"
> >  #include "qemu/sockets.h"
> > @@ -177,10 +178,24 @@ void v9fs_path_free(V9fsPath *path)
> >      g_free(path->data);
> >      path->data = NULL;
> >      path->size = 0;
> >  }
> >  
> > +
> > +void GCC_FMT_ATTR(2, 3)
> > +v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
> > +{
> > +    va_list ap;
> > +
> > +    v9fs_path_free(path);
> > +
> > +    va_start(ap, fmt);
> > +    /* Bump the size for including terminating NULL */
> > +    path->size = g_vasprintf(&path->data, fmt, ap) + 1;
> > +    va_end(ap);
> > +}
> > +
> >  void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
> >  {
> >      v9fs_path_free(lhs);
> >      lhs->data = g_malloc(rhs->size);
> >      memcpy(lhs->data, rhs->data, rhs->size);
> > @@ -915,14 +930,12 @@ static void print_sg(struct iovec *sg, int cnt)
> >  static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
> >  {
> >      V9fsPath str;
> >      v9fs_path_init(&str);
> >      v9fs_path_copy(&str, dst);
> > -    v9fs_string_sprintf((V9fsString *)dst, "%s%s", src->data, str.data+len);
> > +    v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
> >      v9fs_path_free(&str);
> > -    /* +1 to include terminating NULL */
> > -    dst->size++;
> >  }
> >  
> >  static inline bool is_ro_export(FsContext *ctx)
> >  {
> >      return ctx->export_flags & V9FS_RDONLY;
> > diff --git a/hw/9pfs/9p.h b/hw/9pfs/9p.h
> > index a38603398ef5..d539d2ebe9c0 100644
> > --- a/hw/9pfs/9p.h
> > +++ b/hw/9pfs/9p.h
> > @@ -325,10 +325,11 @@ static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
> >  }
> >  
> >  extern void v9fs_reclaim_fd(V9fsPDU *pdu);
> >  extern void v9fs_path_init(V9fsPath *path);
> >  extern void v9fs_path_free(V9fsPath *path);
> > +extern void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
> >  extern void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs);
> >  extern int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
> >                               const char *name, V9fsPath *path);
> >  extern int v9fs_device_realize_common(V9fsState *s, Error **errp);
> >  extern void v9fs_device_unrealize_common(V9fsState *s, Error **errp);
> > 
> >   
>
diff mbox

Patch

diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
index 3f271fcbd2c5..845675e7a1bb 100644
--- a/hw/9pfs/9p-local.c
+++ b/hw/9pfs/9p-local.c
@@ -1058,17 +1058,14 @@  static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
 
 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
                               const char *name, V9fsPath *target)
 {
     if (dir_path) {
-        v9fs_string_sprintf((V9fsString *)target, "%s/%s",
-                            dir_path->data, name);
+        v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
     } else {
-        v9fs_string_sprintf((V9fsString *)target, "%s", name);
+        v9fs_path_sprintf(target, "%s", name);
     }
-    /* Bump the size for including terminating NULL */
-    target->size++;
     return 0;
 }
 
 static int local_renameat(FsContext *ctx, V9fsPath *olddir,
                           const char *old_name, V9fsPath *newdir,
diff --git a/hw/9pfs/9p-proxy.c b/hw/9pfs/9p-proxy.c
index d091564b6fd2..f2417b7fd73d 100644
--- a/hw/9pfs/9p-proxy.c
+++ b/hw/9pfs/9p-proxy.c
@@ -1021,17 +1021,14 @@  static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
 
 static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
                               const char *name, V9fsPath *target)
 {
     if (dir_path) {
-        v9fs_string_sprintf((V9fsString *)target, "%s/%s",
-                            dir_path->data, name);
+        v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
     } else {
-        v9fs_string_sprintf((V9fsString *)target, "%s", name);
+        v9fs_path_sprintf(target, "%s", name);
     }
-    /* Bump the size for including terminating NULL */
-    target->size++;
     return 0;
 }
 
 static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
                           const char *old_name, V9fsPath *newdir,
diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c
index d8f48ca76c47..639f93930285 100644
--- a/hw/9pfs/9p.c
+++ b/hw/9pfs/9p.c
@@ -10,10 +10,11 @@ 
  * the COPYING file in the top-level directory.
  *
  */
 
 #include "qemu/osdep.h"
+#include <glib/gprintf.h>
 #include "hw/virtio/virtio.h"
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/iov.h"
 #include "qemu/sockets.h"
@@ -177,10 +178,24 @@  void v9fs_path_free(V9fsPath *path)
     g_free(path->data);
     path->data = NULL;
     path->size = 0;
 }
 
+
+void GCC_FMT_ATTR(2, 3)
+v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
+{
+    va_list ap;
+
+    v9fs_path_free(path);
+
+    va_start(ap, fmt);
+    /* Bump the size for including terminating NULL */
+    path->size = g_vasprintf(&path->data, fmt, ap) + 1;
+    va_end(ap);
+}
+
 void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
 {
     v9fs_path_free(lhs);
     lhs->data = g_malloc(rhs->size);
     memcpy(lhs->data, rhs->data, rhs->size);
@@ -915,14 +930,12 @@  static void print_sg(struct iovec *sg, int cnt)
 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
 {
     V9fsPath str;
     v9fs_path_init(&str);
     v9fs_path_copy(&str, dst);
-    v9fs_string_sprintf((V9fsString *)dst, "%s%s", src->data, str.data+len);
+    v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
     v9fs_path_free(&str);
-    /* +1 to include terminating NULL */
-    dst->size++;
 }
 
 static inline bool is_ro_export(FsContext *ctx)
 {
     return ctx->export_flags & V9FS_RDONLY;
diff --git a/hw/9pfs/9p.h b/hw/9pfs/9p.h
index a38603398ef5..d539d2ebe9c0 100644
--- a/hw/9pfs/9p.h
+++ b/hw/9pfs/9p.h
@@ -325,10 +325,11 @@  static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
 }
 
 extern void v9fs_reclaim_fd(V9fsPDU *pdu);
 extern void v9fs_path_init(V9fsPath *path);
 extern void v9fs_path_free(V9fsPath *path);
+extern void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
 extern void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs);
 extern int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
                              const char *name, V9fsPath *path);
 extern int v9fs_device_realize_common(V9fsState *s, Error **errp);
 extern void v9fs_device_unrealize_common(V9fsState *s, Error **errp);