diff mbox

[v4,06/47] Add qemu_get_counted_string to read a string prefixed by a count byte

Message ID 1412358473-31398-7-git-send-email-dgilbert@redhat.com
State New
Headers show

Commit Message

Dr. David Alan Gilbert Oct. 3, 2014, 5:47 p.m. UTC
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>

and use it in loadvm_state.

Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
---
 include/migration/qemu-file.h |  2 ++
 qemu-file.c                   | 15 +++++++++++++++
 savevm.c                      | 18 ++++++++++--------
 3 files changed, 27 insertions(+), 8 deletions(-)

Comments

David Gibson Nov. 3, 2014, 2:39 a.m. UTC | #1
On Fri, Oct 03, 2014 at 06:47:12PM +0100, Dr. David Alan Gilbert (git) wrote:
> From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
> 
> and use it in loadvm_state.
> 
> Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> ---
>  include/migration/qemu-file.h |  2 ++
>  qemu-file.c                   | 15 +++++++++++++++
>  savevm.c                      | 18 ++++++++++--------
>  3 files changed, 27 insertions(+), 8 deletions(-)
> 
> diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h
> index 6ef8ebc..a8cac7a 100644
> --- a/include/migration/qemu-file.h
> +++ b/include/migration/qemu-file.h
> @@ -300,4 +300,6 @@ static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv)
>  {
>      qemu_get_be64s(f, (uint64_t *)pv);
>  }
> +
> +int qemu_get_counted_string(QEMUFile *f, uint8_t *buf);

I'd suggest writing the prototype as

int qemu_get_counted_string(QEMUFile *f, uint8_t buf[256]);

The compiled code will be identical, of course, but it helps to
document what the function expects.


>  #endif
> diff --git a/qemu-file.c b/qemu-file.c
> index ccc516c..a057b3e 100644
> --- a/qemu-file.c
> +++ b/qemu-file.c
> @@ -879,6 +879,21 @@ uint64_t qemu_get_be64(QEMUFile *f)
>      return v;
>  }
>  
> +/*
> + * Get a string whose length is determined by a single preceding byte
> + * A preallocated 256 byte buffer must be passed in.
> + * Returns: 0 on success and a 0 terminated string in the buffer
> + */
> +int qemu_get_counted_string(QEMUFile *f, uint8_t *buf)
> +{
> +    unsigned int len = qemu_get_byte(f);
> +    int res = qemu_get_buffer(f, buf, len);
> +
> +    buf[len] = 0;
> +
> +    return res != len;
> +}
> +
>  #define QSB_CHUNK_SIZE      (1 << 10)
>  #define QSB_MAX_CHUNK_SIZE  (16 * QSB_CHUNK_SIZE)
>  
> diff --git a/savevm.c b/savevm.c
> index c3a1f68..cb6f0de 100644
> --- a/savevm.c
> +++ b/savevm.c
> @@ -908,7 +908,7 @@ int qemu_loadvm_state(QEMUFile *f)
>  
>      v = qemu_get_be32(f);
>      if (v == QEMU_VM_FILE_VERSION_COMPAT) {
> -        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
> +        error_report("SaveVM v2 format is obsolete and don't work anymore");

These changes of fprintf() to error_report() look like an unrelated
cleanup.
Dr. David Alan Gilbert Nov. 25, 2014, 4:13 p.m. UTC | #2
* David Gibson (david@gibson.dropbear.id.au) wrote:
> On Fri, Oct 03, 2014 at 06:47:12PM +0100, Dr. David Alan Gilbert (git) wrote:
> > From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
> > 
> > and use it in loadvm_state.
> > 
> > Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> > ---
> >  include/migration/qemu-file.h |  2 ++
> >  qemu-file.c                   | 15 +++++++++++++++
> >  savevm.c                      | 18 ++++++++++--------
> >  3 files changed, 27 insertions(+), 8 deletions(-)
> > 
> > diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h
> > index 6ef8ebc..a8cac7a 100644
> > --- a/include/migration/qemu-file.h
> > +++ b/include/migration/qemu-file.h
> > @@ -300,4 +300,6 @@ static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv)
> >  {
> >      qemu_get_be64s(f, (uint64_t *)pv);
> >  }
> > +
> > +int qemu_get_counted_string(QEMUFile *f, uint8_t *buf);
> 
> I'd suggest writing the prototype as
> 
> int qemu_get_counted_string(QEMUFile *f, uint8_t buf[256]);
> 
> The compiled code will be identical, of course, but it helps to
> document what the function expects.

Good idea; done.

> >  #endif
> > diff --git a/qemu-file.c b/qemu-file.c
> > index ccc516c..a057b3e 100644
> > --- a/qemu-file.c
> > +++ b/qemu-file.c
> > @@ -879,6 +879,21 @@ uint64_t qemu_get_be64(QEMUFile *f)
> >      return v;
> >  }
> >  
> > +/*
> > + * Get a string whose length is determined by a single preceding byte
> > + * A preallocated 256 byte buffer must be passed in.
> > + * Returns: 0 on success and a 0 terminated string in the buffer
> > + */
> > +int qemu_get_counted_string(QEMUFile *f, uint8_t *buf)
> > +{
> > +    unsigned int len = qemu_get_byte(f);
> > +    int res = qemu_get_buffer(f, buf, len);
> > +
> > +    buf[len] = 0;
> > +
> > +    return res != len;
> > +}
> > +
> >  #define QSB_CHUNK_SIZE      (1 << 10)
> >  #define QSB_MAX_CHUNK_SIZE  (16 * QSB_CHUNK_SIZE)
> >  
> > diff --git a/savevm.c b/savevm.c
> > index c3a1f68..cb6f0de 100644
> > --- a/savevm.c
> > +++ b/savevm.c
> > @@ -908,7 +908,7 @@ int qemu_loadvm_state(QEMUFile *f)
> >  
> >      v = qemu_get_be32(f);
> >      if (v == QEMU_VM_FILE_VERSION_COMPAT) {
> > -        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
> > +        error_report("SaveVM v2 format is obsolete and don't work anymore");
> 
> These changes of fprintf() to error_report() look like an unrelated
> cleanup.

Not quite;   with the use of qemu_get_counted_string it can return an error,
so I check it, and use error_report; while I was there I converted
a couple of the surrounding fprintf's to error_report at the same time.

Dave

> 
> -- 
> David Gibson			| I'll have my music baroque, and my code
> david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
> 				| _way_ _around_!
> http://www.ozlabs.org/~dgibson


--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
diff mbox

Patch

diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h
index 6ef8ebc..a8cac7a 100644
--- a/include/migration/qemu-file.h
+++ b/include/migration/qemu-file.h
@@ -300,4 +300,6 @@  static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv)
 {
     qemu_get_be64s(f, (uint64_t *)pv);
 }
+
+int qemu_get_counted_string(QEMUFile *f, uint8_t *buf);
 #endif
diff --git a/qemu-file.c b/qemu-file.c
index ccc516c..a057b3e 100644
--- a/qemu-file.c
+++ b/qemu-file.c
@@ -879,6 +879,21 @@  uint64_t qemu_get_be64(QEMUFile *f)
     return v;
 }
 
+/*
+ * Get a string whose length is determined by a single preceding byte
+ * A preallocated 256 byte buffer must be passed in.
+ * Returns: 0 on success and a 0 terminated string in the buffer
+ */
+int qemu_get_counted_string(QEMUFile *f, uint8_t *buf)
+{
+    unsigned int len = qemu_get_byte(f);
+    int res = qemu_get_buffer(f, buf, len);
+
+    buf[len] = 0;
+
+    return res != len;
+}
+
 #define QSB_CHUNK_SIZE      (1 << 10)
 #define QSB_MAX_CHUNK_SIZE  (16 * QSB_CHUNK_SIZE)
 
diff --git a/savevm.c b/savevm.c
index c3a1f68..cb6f0de 100644
--- a/savevm.c
+++ b/savevm.c
@@ -908,7 +908,7 @@  int qemu_loadvm_state(QEMUFile *f)
 
     v = qemu_get_be32(f);
     if (v == QEMU_VM_FILE_VERSION_COMPAT) {
-        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
+        error_report("SaveVM v2 format is obsolete and don't work anymore");
         return -ENOTSUP;
     }
     if (v != QEMU_VM_FILE_VERSION) {
@@ -918,31 +918,33 @@  int qemu_loadvm_state(QEMUFile *f)
     while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
         uint32_t instance_id, version_id, section_id;
         SaveStateEntry *se;
-        char idstr[257];
-        int len;
+        char idstr[256];
 
         switch (section_type) {
         case QEMU_VM_SECTION_START:
         case QEMU_VM_SECTION_FULL:
             /* Read section start */
             section_id = qemu_get_be32(f);
-            len = qemu_get_byte(f);
-            qemu_get_buffer(f, (uint8_t *)idstr, len);
-            idstr[len] = 0;
+            if (qemu_get_counted_string(f, (uint8_t *)idstr)) {
+                error_report("Unable to read ID string for section %u",
+                            section_id);
+                return -EINVAL;
+            }
             instance_id = qemu_get_be32(f);
             version_id = qemu_get_be32(f);
 
             /* Find savevm section */
             se = find_se(idstr, instance_id);
             if (se == NULL) {
-                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
+                error_report("Unknown savevm section or instance '%s' %d",
+                             idstr, instance_id);
                 ret = -EINVAL;
                 goto out;
             }
 
             /* Validate version */
             if (version_id > se->version_id) {
-                fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
+                error_report("savevm: unsupported version %d for '%s' v%d",
                         version_id, idstr, se->version_id);
                 ret = -EINVAL;
                 goto out;