Skip to contentSkip to conversation

error: Eliminate QERR_PROPERTY_VALUE_OUT_OF_RANGE

PATCH In review Markus Armbruster 3 participants 18 messages Oct 10, 2024 – Oct 17, 2024 Block layer

lore
Share & tools
lore
#0 lore raw

v2 cover error: Eliminate QERR_PROPERTY_VALUE_OUT_OF_RANGE

Markus Armbruster (7):
error: Drop superfluous #include "qapi/qmp/qerror.h"
block: Improve errors about block sizes
block: Adjust check_block_size() signature target/i386/cpu: Avoid mixing signed and unsigned in property setters target/i386/cpu: Improve errors for out of bounds property values hw/intc/openpic: Improve errors for out of bounds property values
qerror: QERR_PROPERTY_VALUE_OUT_OF_RANGE is no longer used, drop

 include/qapi/qmp/qerror.h            |  3 --
 util/block-helpers.h                 |  3 +-
 block/export/vduse-blk.c             |  7 +---
 block/export/vhost-user-blk-server.c |  6 +--
 hw/core/qdev-properties-system.c     |  6 +--
 hw/intc/openpic.c                    |  5 +--
 qga/commands-bsd.c                   |  1 -
 qga/commands-linux.c                 |  1 -
 qga/commands-posix.c                 |  1 -
 target/i386/cpu.c                    | 59 +++++++++++++---------------
 util/block-helpers.c                 | 28 ++++++-------
 11 files changed, 46 insertions(+), 74 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH v2 1/7 error: Drop superfluous #include "qapi/qmp/qerror.h"

---
 qga/commands-bsd.c   | 1 -
 qga/commands-linux.c | 1 -
 qga/commands-posix.c | 1 -
 3 files changed, 3 deletions(-)
qga/commands-bsd.c +0 -1
index 9ce48af311..94ff6fee6a 100644
@@ -12,7 +12,6 @@
 
 #include "qemu/osdep.h"
 #include "qga-qapi-commands.h"
-#include "qapi/qmp/qerror.h"
 #include "qapi/error.h"
 #include "qemu/queue.h"
 #include "commands-common.h"
qga/commands-linux.c +0 -1
index 51d5e3d927..9b1746b24f 100644
@@ -15,7 +15,6 @@
 #include "qapi/error.h"
 #include "qga-qapi-commands.h"
 #include "qapi/error.h"
-#include "qapi/qmp/qerror.h"
 #include "commands-common.h"
 #include "cutils.h"
 #include <mntent.h>
qga/commands-posix.c +0 -1
index c2bd0b4316..389c5eeb5d 100644
@@ -18,7 +18,6 @@
 #include <dirent.h>
 #include "qga-qapi-commands.h"
 #include "qapi/error.h"
-#include "qapi/qmp/qerror.h"
 #include "qemu/host-utils.h"
 #include "qemu/sockets.h"
 #include "qemu/base64.h"
Signature · 1 line
2.46.0
#2 lore raw

PATCH v2 2/7 block: Improve errors about block sizes

In reply to Markus Armbruster

Block sizes need to be a power of two between 512 and an arbitrary limit, currently 2MiB.

Commit 5937835ac4c factored block size checking out of set_blocksize() into new check_block_size(), for reuse in block/export/.

Its two error messages are okay for the original purpose:

    $ qemu-system-x86_64 -device ide-hd,physical_block_size=1
    qemu-system-x86_64: -device ide-hd,physical_block_size=1: Property .physical_block_size doesn't take value 1 (minimum: 512, maximum: 2097152)
    $ qemu-system-x86_64 -device ide-hd,physical_block_size=513
    qemu-system-x86_64: -device ide-hd,physical_block_size=513: Property .physical_block_size doesn't take value '513', it's not a power of 2

They're mildly off for block exports:

    $ qemu-storage-daemon --blockdev node-name=nod0,driver=file,filename=foo.img --export type=vduse-blk,id=exp0,node-name=nod0,name=foo,logical-block-size=1
    qemu-storage-daemon: --export type=vduse-blk,id=exp0,node-name=nod0,name=foo,logical-block-size=1: Property exp0.logical-block-size doesn't take value 1 (minimum: 512, maximum: 2097152)

The error message talks about a property. CLI options like --export don't have properties, they have parameters.

Replace the two error messages by a single one that's okay for both purposes. Looks like this:

    qemu-storage-daemon: --export type=vduse-blk,id=exp0,node-name=nod0,name=foo,logical-block-size=1: parameter logical-block-size must be a power of 2 between 512 and 2097152
---
 util/block-helpers.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
util/block-helpers.c +7 -11
index c4851432f5..fb5de348e2 100644
@@ -10,7 +10,6 @@
 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
-#include "qapi/qmp/qerror.h"
 #include "block-helpers.h"
 
 /**
@@ -28,19 +27,16 @@
 void check_block_size(const char *id, const char *name, int64_t value,
                       Error **errp)
 {
-    /* value of 0 means "unset" */
-    if (value && (value < MIN_BLOCK_SIZE || value > MAX_BLOCK_SIZE)) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
-                   id, name, value, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE);
+    if (!value) {
+        /* unset */
         return;
     }
 
-    /* We rely on power-of-2 blocksizes for bitmasks */
-    if ((value & (value - 1)) != 0) {
+    if (value < MIN_BLOCK_SIZE || value > MAX_BLOCK_SIZE
+        || (value & (value - 1))) {
         error_setg(errp,
-                   "Property %s.%s doesn't take value '%" PRId64
-                   "', it's not a power of 2",
-                   id, name, value);
-        return;
+                   "parameter %s must be a power of 2 between %" PRId64
+                   " and %" PRId64,
+                   name, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE);
     }
 }
Signature · 1 line
2.46.0
#3 lore raw

PATCH v2 3/7 block: Adjust check_block_size() signature

In reply to Markus Armbruster

Parameter @id is no longer used, drop. Return a bool to indicate success / failure, as recommended by qapi/error.h.

---
 util/block-helpers.h                 |  3 +--
 block/export/vduse-blk.c             |  7 ++-----
 block/export/vhost-user-blk-server.c |  6 +-----
 hw/core/qdev-properties-system.c     |  6 +-----
 util/block-helpers.c                 | 10 ++++++----
 5 files changed, 11 insertions(+), 21 deletions(-)
util/block-helpers.h +1 -2
index b53295a529..838b0825f6 100644
@@ -13,7 +13,6 @@
 #define MAX_BLOCK_SIZE          (2 * MiB)
 #define MAX_BLOCK_SIZE_STR      "2 MiB"
 
-void check_block_size(const char *id, const char *name, int64_t value,
-                      Error **errp);
+bool check_block_size(const char *name, int64_t value, Error **errp);
 
 #endif /* BLOCK_HELPERS_H */
block/export/vduse-blk.c +2 -5
index 172f73cef4..bd852e538d 100644
@@ -273,7 +273,6 @@ static int vduse_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
     uint64_t logical_block_size = VIRTIO_BLK_SECTOR_SIZE;
     uint16_t num_queues = VDUSE_DEFAULT_NUM_QUEUE;
     uint16_t queue_size = VDUSE_DEFAULT_QUEUE_SIZE;
-    Error *local_err = NULL;
     struct virtio_blk_config config = { 0 };
     uint64_t features;
     int i, ret;
@@ -297,10 +296,8 @@ static int vduse_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
 
     if (vblk_opts->has_logical_block_size) {
         logical_block_size = vblk_opts->logical_block_size;
-        check_block_size(exp->id, "logical-block-size", logical_block_size,
-                         &local_err);
-        if (local_err) {
-            error_propagate(errp, local_err);
+        if (!check_block_size("logical-block-size", logical_block_size,
+                              errp)) {
             return -EINVAL;
         }
     }
block/export/vhost-user-blk-server.c +1 -5
index 50c358e8cd..d9d2014d9b 100644
@@ -319,7 +319,6 @@ static int vu_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
 {
     VuBlkExport *vexp = container_of(exp, VuBlkExport, export);
     BlockExportOptionsVhostUserBlk *vu_opts = &opts->u.vhost_user_blk;
-    Error *local_err = NULL;
     uint64_t logical_block_size;
     uint16_t num_queues = VHOST_USER_BLK_NUM_QUEUES_DEFAULT;
 
@@ -330,10 +329,7 @@ static int vu_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,
     } else {
         logical_block_size = VIRTIO_BLK_SECTOR_SIZE;
     }
-    check_block_size(exp->id, "logical-block-size", logical_block_size,
-                     &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    if (!check_block_size("logical-block-size", logical_block_size, errp)) {
         return -EINVAL;
     }
 
hw/core/qdev-properties-system.c +1 -5
index 60bcd821de..35deef05f3 100644
@@ -588,18 +588,14 @@ const PropertyInfo qdev_prop_losttickpolicy = {
 static void set_blocksize(Object *obj, Visitor *v, const char *name,
                           void *opaque, Error **errp)
 {
-    DeviceState *dev = DEVICE(obj);
     Property *prop = opaque;
     uint32_t *ptr = object_field_prop_ptr(obj, prop);
     uint64_t value;
-    Error *local_err = NULL;
 
     if (!visit_type_size(v, name, &value, errp)) {
         return;
     }
-    check_block_size(dev->id ? : "", name, value, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    if (!check_block_size(name, value, errp)) {
         return;
     }
     *ptr = value;
util/block-helpers.c +6 -4
index fb5de348e2..052b4e1476 100644
@@ -14,7 +14,6 @@
 
 /**
  * check_block_size:
- * @id: The unique ID of the object
  * @name: The name of the property being validated
  * @value: The block size in bytes
  * @errp: A pointer to an area to store an error
@@ -23,13 +22,14 @@
  * 1. At least MIN_BLOCK_SIZE
  * 2. No larger than MAX_BLOCK_SIZE
  * 3. A power of 2
+ *
+ * Returns: true on success, false on failure
  */
-void check_block_size(const char *id, const char *name, int64_t value,
-                      Error **errp)
+bool check_block_size(const char *name, int64_t value, Error **errp)
 {
     if (!value) {
         /* unset */
-        return;
+        return true;
     }
 
     if (value < MIN_BLOCK_SIZE || value > MAX_BLOCK_SIZE
@@ -38,5 +38,7 @@ void check_block_size(const char *id, const char *name, int64_t value,
                    "parameter %s must be a power of 2 between %" PRId64
                    " and %" PRId64,
                    name, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE);
+        return false;
     }
+    return true;
 }
Signature · 1 line
2.46.0
Reviewed-by on v2 3/7
#4 lore raw

Re: [PATCH v2 3/7] block: Adjust check_block_size() signature

Quoted 11 lines · Markus Armbruster

Parameter @id is no longer used, drop. Return a bool to indicate success / failure, as recommended by qapi/error.h.

---
  util/block-helpers.h                 |  3 +--
  block/export/vduse-blk.c             |  7 ++-----
  block/export/vhost-user-blk-server.c |  6 +-----
  hw/core/qdev-properties-system.c     |  6 +-----
  util/block-helpers.c                 | 10 ++++++----
  5 files changed, 11 insertions(+), 21 deletions(-)
#5 lore raw

PATCH v2 4/7 target/i386/cpu: Avoid mixing signed and unsigned in property setters

In reply to Markus Armbruster

Properties "family", "model", and "stepping" are visited as signed integers. They are backed by bits in CPUX86State member @cpuid_version. The code to extract and insert these bits mixes signed and unsigned. Not actually wrong, but avoiding such mixing is good practice.

Visit them as unsigned integers instead.

This adds a few mildly ugly cast in arguments of error_setg(). The next commit will get rid of them again.

Property "tsc-frequency" is also visited as signed integer. The value ultimately flows into the kernel, where it is 31 bits unsigned. The QEMU code freely mixes int, uint32_t, int64_t. I elect not to attempt draining this swamp today.

---
 target/i386/cpu.c | 45 +++++++++++++++++++++------------------------
 1 file changed, 21 insertions(+), 24 deletions(-)
target/i386/cpu.c +21 -24
index ff227a8c5c..4f8fa60432 100644
@@ -5433,13 +5433,13 @@ static void x86_cpuid_version_get_family(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    int64_t value;
+    uint64_t value;
 
     value = (env->cpuid_version >> 8) & 0xf;
     if (value == 0xf) {
         value += (env->cpuid_version >> 20) & 0xff;
     }
-    visit_type_int(v, name, &value, errp);
+    visit_type_uint64(v, name, &value, errp);
 }
 
 static void x86_cpuid_version_set_family(Object *obj, Visitor *v,
@@ -5448,16 +5448,15 @@ static void x86_cpuid_version_set_family(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    const int64_t min = 0;
-    const int64_t max = 0xff + 0xf;
-    int64_t value;
+    const uint64_t max = 0xff + 0xf;
+    uint64_t value;
 
-    if (!visit_type_int(v, name, &value, errp)) {
+    if (!visit_type_uint64(v, name, &value, errp)) {
         return;
     }
-    if (value < min || value > max) {
+    if (value > max) {
         error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+                   name ? name : "null", value, (int64_t)0, (int64_t)max);
         return;
     }
 
@@ -5475,11 +5474,11 @@ static void x86_cpuid_version_get_model(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    int64_t value;
+    uint64_t value;
 
     value = (env->cpuid_version >> 4) & 0xf;
     value |= ((env->cpuid_version >> 16) & 0xf) << 4;
-    visit_type_int(v, name, &value, errp);
+    visit_type_uint64(v, name, &value, errp);
 }
 
 static void x86_cpuid_version_set_model(Object *obj, Visitor *v,
@@ -5488,16 +5487,15 @@ static void x86_cpuid_version_set_model(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    const int64_t min = 0;
-    const int64_t max = 0xff;
-    int64_t value;
+    const uint64_t max = 0xff;
+    uint64_t value;
 
-    if (!visit_type_int(v, name, &value, errp)) {
+    if (!visit_type_uint64(v, name, &value, errp)) {
         return;
     }
-    if (value < min || value > max) {
+    if (value > max) {
         error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+                   name ? name : "null", value, (int64_t)0, (int64_t)max);
         return;
     }
 
@@ -5511,10 +5509,10 @@ static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    int64_t value;
+    uint64_t value;
 
     value = env->cpuid_version & 0xf;
-    visit_type_int(v, name, &value, errp);
+    visit_type_uint64(v, name, &value, errp);
 }
 
 static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
@@ -5523,16 +5521,15 @@ static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    const int64_t min = 0;
-    const int64_t max = 0xf;
-    int64_t value;
+    const uint64_t max = 0xf;
+    uint64_t value;
 
-    if (!visit_type_int(v, name, &value, errp)) {
+    if (!visit_type_uint64(v, name, &value, errp)) {
         return;
     }
-    if (value < min || value > max) {
+    if (value > max) {
         error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+                   name ? name : "null", value, (int64_t)0, (int64_t)max);
         return;
     }
 
Signature · 1 line
2.46.0
Reviewed-by on v2 4/7
#6 lore raw

Re: [PATCH v2 4/7] target/i386/cpu: Avoid mixing signed and unsigned in property setters

Quoted 11 lines · On Thu, 10 Oct 2024 17:01:41 +0200 Markus Armbruster <[email protected]> wrote:

Properties "family", "model", and "stepping" are visited as signed integers. They are backed by bits in CPUX86State member @cpuid_version. The code to extract and insert these bits mixes signed and unsigned. Not actually wrong, but avoiding such mixing is good practice.

Visit them as unsigned integers instead.

This adds a few mildly ugly cast in arguments of error_setg(). The next commit will get rid of them again.

Quoted 6 lines

Property "tsc-frequency" is also visited as signed integer. The value ultimately flows into the kernel, where it is 31 bits unsigned. The QEMU code freely mixes int, uint32_t, int64_t. I elect not to attempt draining this swamp today.

Quoted 117 lines
---
 target/i386/cpu.c | 45 +++++++++++++++++++++------------------------
 1 file changed, 21 insertions(+), 24 deletions(-)
target/i386/cpu.c +21 -24
index ff227a8c5c..4f8fa60432 100644
@@ -5433,13 +5433,13 @@ static void x86_cpuid_version_get_family(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    int64_t value;
+    uint64_t value;
 
     value = (env->cpuid_version >> 8) & 0xf;
     if (value == 0xf) {
         value += (env->cpuid_version >> 20) & 0xff;
     }
-    visit_type_int(v, name, &value, errp);
+    visit_type_uint64(v, name, &value, errp);
 }
 
 static void x86_cpuid_version_set_family(Object *obj, Visitor *v,
@@ -5448,16 +5448,15 @@ static void x86_cpuid_version_set_family(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    const int64_t min = 0;
-    const int64_t max = 0xff + 0xf;
-    int64_t value;
+    const uint64_t max = 0xff + 0xf;
+    uint64_t value;
 
-    if (!visit_type_int(v, name, &value, errp)) {
+    if (!visit_type_uint64(v, name, &value, errp)) {
         return;
     }
-    if (value < min || value > max) {
+    if (value > max) {
         error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+                   name ? name : "null", value, (int64_t)0, (int64_t)max);
         return;
     }
 
@@ -5475,11 +5474,11 @@ static void x86_cpuid_version_get_model(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    int64_t value;
+    uint64_t value;
 
     value = (env->cpuid_version >> 4) & 0xf;
     value |= ((env->cpuid_version >> 16) & 0xf) << 4;
-    visit_type_int(v, name, &value, errp);
+    visit_type_uint64(v, name, &value, errp);
 }
 
 static void x86_cpuid_version_set_model(Object *obj, Visitor *v,
@@ -5488,16 +5487,15 @@ static void x86_cpuid_version_set_model(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    const int64_t min = 0;
-    const int64_t max = 0xff;
-    int64_t value;
+    const uint64_t max = 0xff;
+    uint64_t value;
 
-    if (!visit_type_int(v, name, &value, errp)) {
+    if (!visit_type_uint64(v, name, &value, errp)) {
         return;
     }
-    if (value < min || value > max) {
+    if (value > max) {
         error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+                   name ? name : "null", value, (int64_t)0, (int64_t)max);
         return;
     }
 
@@ -5511,10 +5509,10 @@ static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    int64_t value;
+    uint64_t value;
 
     value = env->cpuid_version & 0xf;
-    visit_type_int(v, name, &value, errp);
+    visit_type_uint64(v, name, &value, errp);
 }
 
 static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
@@ -5523,16 +5521,15 @@ static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
-    const int64_t min = 0;
-    const int64_t max = 0xf;
-    int64_t value;
+    const uint64_t max = 0xf;
+    uint64_t value;
 
-    if (!visit_type_int(v, name, &value, errp)) {
+    if (!visit_type_uint64(v, name, &value, errp)) {
         return;
     }
-    if (value < min || value > max) {
+    if (value > max) {
         error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+                   name ? name : "null", value, (int64_t)0, (int64_t)max);
         return;
     }
 
#7 lore raw

PATCH v2 5/7 target/i386/cpu: Improve errors for out of bounds property values

In reply to Markus Armbruster

The error message for a "stepping" value that is out of bounds is a bit odd:

    $ qemu-system-x86_64 -cpu qemu64,stepping=16
    qemu-system-x86_64: can't apply global qemu64-x86_64-cpu.stepping=16: Property .stepping doesn't take value 16 (minimum: 0, maximum: 15)

The "can't apply global" part is an unfortunate artifact of -cpu's implementation. Left for another day.

The remainder feels overly verbose. Change it to

    qemu64-x86_64-cpu: can't apply global qemu64-x86_64-cpu.stepping=16: parameter 'stepping' can be at most 15

Likewise for "family", "model", and "tsc-frequency".

---
 target/i386/cpu.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
target/i386/cpu.c +9 -11
index 4f8fa60432..de2c7041c5 100644
@@ -31,7 +31,6 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qapi/qapi-visit-machine.h"
-#include "qapi/qmp/qerror.h"
 #include "standard-headers/asm-x86/kvm_para.h"
 #include "hw/qdev-properties.h"
 #include "hw/i386/topology.h"
@@ -5455,8 +5454,8 @@ static void x86_cpuid_version_set_family(Object *obj, Visitor *v,
         return;
     }
     if (value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, (int64_t)0, (int64_t)max);
+        error_setg(errp, "parameter '%s' can be at most %" PRIu64,
+                   name ? name : "null", max);
         return;
     }
 
@@ -5494,8 +5493,8 @@ static void x86_cpuid_version_set_model(Object *obj, Visitor *v,
         return;
     }
     if (value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, (int64_t)0, (int64_t)max);
+        error_setg(errp, "parameter '%s' can be at most %" PRIu64,
+                   name ? name : "null", max);
         return;
     }
 
@@ -5528,8 +5527,8 @@ static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
         return;
     }
     if (value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, (int64_t)0, (int64_t)max);
+        error_setg(errp, "parameter '%s' can be at most %" PRIu64,
+                   name ? name : "null", max);
         return;
     }
 
@@ -5623,16 +5622,15 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
     X86CPU *cpu = X86_CPU(obj);
-    const int64_t min = 0;
     const int64_t max = INT64_MAX;
     int64_t value;
 
     if (!visit_type_int(v, name, &value, errp)) {
         return;
     }
-    if (value < min || value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+    if (value < 0 || value > max) {
+        error_setg(errp, "parameter '%s' can be at most %" PRId64,
+                   name ? name : "null", max);
         return;
     }
 
Signature · 1 line
2.46.0
#8 lore raw

Re: [PATCH v2 5/7] target/i386/cpu: Improve errors for out of bounds property values

Quoted 28 lines · Markus Armbruster

The error message for a "stepping" value that is out of bounds is a bit odd:

     $ qemu-system-x86_64 -cpu qemu64,stepping=16
     qemu-system-x86_64: can't apply global qemu64-x86_64-cpu.stepping=16: Property .stepping doesn't take value 16 (minimum: 0, maximum: 15)

The "can't apply global" part is an unfortunate artifact of -cpu's implementation. Left for another day.

The remainder feels overly verbose. Change it to

     qemu64-x86_64-cpu: can't apply global qemu64-x86_64-cpu.stepping=16: parameter 'stepping' can be at most 15

Likewise for "family", "model", and "tsc-frequency".

---
  target/i386/cpu.c | 20 +++++++++-----------
  1 file changed, 9 insertions(+), 11 deletions(-)
target/i386/cpu.c +0 -1
@@ -5623,16 +5622,15 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, const char *name,
                                     void *opaque, Error **errp)
  {
      X86CPU *cpu = X86_CPU(obj);
-    const int64_t min = 0;
      const int64_t max = INT64_MAX;
      int64_t value;

target/i386/cpu.c static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, const char *name, · line 5633

Quoted 10 lines
target/i386/cpu.c +3 -3
 
      if (!visit_type_int(v, name, &value, errp)) {
          return;
      }
-    if (value < min || value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+    if (value < 0 || value > max) {
+        error_setg(errp, "parameter '%s' can be at most %" PRId64,
+                   name ? name : "null", max);

Confusing:

    qemu64-x86_64-cpu: can't apply global 

qemu64-x86_64-cpu.stepping=-1: parameter 'stepping' can be at most 15

Quoted 3 lines
(quoted diff) +0 -0
          return;
      }
 
#9 lore raw

Re: [PATCH v2 5/7] target/i386/cpu: Improve errors for out of bounds property values

Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Markus Armbruster

The error message for a "stepping" value that is out of bounds is a bit odd:

     $ qemu-system-x86_64 -cpu qemu64,stepping=16
     qemu-system-x86_64: can't apply global qemu64-x86_64-cpu.stepping=16: Property .stepping doesn't take value 16 (minimum: 0, maximum: 15)
The "can't apply global" part is an unfortunate artifact of -cpu's
implementation.  Left for another day.
The remainder feels overly verbose.  Change it to
     qemu64-x86_64-cpu: can't apply global qemu64-x86_64-cpu.stepping=16: parameter 'stepping' can be at most 15
Likewise for "family", "model", and "tsc-frequency".
Signed-off-by: Markus Armbruster <[email protected]>
---
  target/i386/cpu.c | 20 +++++++++-----------
  1 file changed, 9 insertions(+), 11 deletions(-)
target/i386/cpu.c +0 -1
@@ -5623,16 +5622,15 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, const char *name,
                                     void *opaque, Error **errp)
  {
      X86CPU *cpu = X86_CPU(obj);
-    const int64_t min = 0;
      const int64_t max = INT64_MAX;

target/i386/cpu.c static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, const char *name, · line 5632

Quoted 10 lines
target/i386/cpu.c +3 -3
      int64_t value;
        if (!visit_type_int(v, name, &value, errp)) {
          return;
      }
-    if (value < min || value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+    if (value < 0 || value > max) {
+        error_setg(errp, "parameter '%s' can be at most %" PRId64,
+                   name ? name : "null", max);

Confusing:

Quoted 1 line
    qemu64-x86_64-cpu: can't apply global qemu64-x86_64-cpu.stepping=-1: parameter 'stepping' can be at most 15

For better or worse, visit_type_uint64() with the string input visitor parses -1 modulo 2^64, i.e. as 2^64-1, just like strtoul() & friends.

Quoted 3 lines
Quoted 3 lines
(quoted diff) +0 -0
          return;
      }
 
#10 lore raw

Re: [PATCH v2 5/7] target/i386/cpu: Improve errors for out of bounds property values

Quoted 37 lines · Markus Armbruster
Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Markus Armbruster

The error message for a "stepping" value that is out of bounds is a bit odd:

      $ qemu-system-x86_64 -cpu qemu64,stepping=16
      qemu-system-x86_64: can't apply global qemu64-x86_64-cpu.stepping=16: Property .stepping doesn't take value 16 (minimum: 0, maximum: 15)
The "can't apply global" part is an unfortunate artifact of -cpu's
implementation.  Left for another day.
The remainder feels overly verbose.  Change it to
      qemu64-x86_64-cpu: can't apply global qemu64-x86_64-cpu.stepping=16: parameter 'stepping' can be at most 15
Likewise for "family", "model", and "tsc-frequency".
Signed-off-by: Markus Armbruster <[email protected]>
---
   target/i386/cpu.c | 20 +++++++++-----------
   1 file changed, 9 insertions(+), 11 deletions(-)
target/i386/cpu.c +0 -1
@@ -5623,16 +5622,15 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, const char *name,
                                      void *opaque, Error **errp)
   {
       X86CPU *cpu = X86_CPU(obj);
-    const int64_t min = 0;
       const int64_t max = INT64_MAX;

target/i386/cpu.c static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, const char *name, · line 5632

Quoted 10 lines
target/i386/cpu.c +3 -3
       int64_t value;
         if (!visit_type_int(v, name, &value, errp)) {
           return;
       }
-    if (value < min || value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+    if (value < 0 || value > max) {
+        error_setg(errp, "parameter '%s' can be at most %" PRId64,
+                   name ? name : "null", max);

Confusing:

Quoted 4 lines
Quoted 1 line
     qemu64-x86_64-cpu: can't apply global qemu64-x86_64-cpu.stepping=-1: parameter 'stepping' can be at most 15

For better or worse, visit_type_uint64() with the string input visitor parses -1 modulo 2^64, i.e. as 2^64-1, just like strtoul() & friends.

Would "parameter 'stepping' must be between 1 and 15" be clearer?

#11 lore raw

Re: [PATCH v2 5/7] target/i386/cpu: Improve errors for out of bounds property values

Quoted 14 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Markus Armbruster
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Markus Armbruster

The error message for a "stepping" value that is out of bounds is a bit odd:

      $ qemu-system-x86_64 -cpu qemu64,stepping=16
      qemu-system-x86_64: can't apply global qemu64-x86_64-cpu.stepping=16: Property .stepping doesn't take value 16 (minimum: 0, maximum: 15)
The "can't apply global" part is an unfortunate artifact of -cpu's
implementation.  Left for another day.
The remainder feels overly verbose.  Change it to
      qemu64-x86_64-cpu: can't apply global qemu64-x86_64-cpu.stepping=16: parameter 'stepping' can be at most 15
Likewise for "family", "model", and "tsc-frequency".
Signed-off-by: Markus Armbruster <[email protected]>

[...]

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Confusing:

     qemu64-x86_64-cpu: can't apply global qemu64-x86_64-cpu.stepping=-1: parameter 'stepping' can be at most 15

For better or worse, visit_type_uint64() with the string input visitor parses -1 modulo 2^64, i.e. as 2^64-1, just like strtoul() & friends.

I wish we had avoided that design mistake. Likely too late to fix now. The JSON parser gets it right.

Quoted 1 line

Would "parameter 'stepping' must be between 1 and 15" be clearer?

It might be clearer and would be wronger: zero is a valid value.

I could do "must be between 0 and 15". But "stepping" is a *counter*. A negative stepping makes no sense to me.

Same for model and family.

More so for tsc-frequency.

Thoughts?

Reviewed-by on v2 5/7
#12 lore raw

Re: [PATCH v2 5/7] target/i386/cpu: Improve errors for out of bounds property values

In reply to Markus Armbruster
Quoted 9 lines · On Thu, 10 Oct 2024 17:01:42 +0200 Markus Armbruster <[email protected]> wrote:

The error message for a "stepping" value that is out of bounds is a bit odd:

    $ qemu-system-x86_64 -cpu qemu64,stepping=16
    qemu-system-x86_64: can't apply global qemu64-x86_64-cpu.stepping=16: Property .stepping doesn't take value 16 (minimum: 0, maximum: 15)

The "can't apply global" part is an unfortunate artifact of -cpu's implementation. Left for another day.

Quoted 7 lines

The remainder feels overly verbose. Change it to

    qemu64-x86_64-cpu: can't apply global qemu64-x86_64-cpu.stepping=16: parameter 'stepping' can be at most 15

Likewise for "family", "model", and "tsc-frequency".

Quoted 69 lines
---
 target/i386/cpu.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
target/i386/cpu.c +9 -11
index 4f8fa60432..de2c7041c5 100644
@@ -31,7 +31,6 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qapi/qapi-visit-machine.h"
-#include "qapi/qmp/qerror.h"
 #include "standard-headers/asm-x86/kvm_para.h"
 #include "hw/qdev-properties.h"
 #include "hw/i386/topology.h"
@@ -5455,8 +5454,8 @@ static void x86_cpuid_version_set_family(Object *obj, Visitor *v,
         return;
     }
     if (value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, (int64_t)0, (int64_t)max);
+        error_setg(errp, "parameter '%s' can be at most %" PRIu64,
+                   name ? name : "null", max);
         return;
     }
 
@@ -5494,8 +5493,8 @@ static void x86_cpuid_version_set_model(Object *obj, Visitor *v,
         return;
     }
     if (value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, (int64_t)0, (int64_t)max);
+        error_setg(errp, "parameter '%s' can be at most %" PRIu64,
+                   name ? name : "null", max);
         return;
     }
 
@@ -5528,8 +5527,8 @@ static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
         return;
     }
     if (value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, (int64_t)0, (int64_t)max);
+        error_setg(errp, "parameter '%s' can be at most %" PRIu64,
+                   name ? name : "null", max);
         return;
     }
 
@@ -5623,16 +5622,15 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
     X86CPU *cpu = X86_CPU(obj);
-    const int64_t min = 0;
     const int64_t max = INT64_MAX;
     int64_t value;
 
     if (!visit_type_int(v, name, &value, errp)) {
         return;
     }
-    if (value < min || value > max) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
-                   name ? name : "null", value, min, max);
+    if (value < 0 || value > max) {
+        error_setg(errp, "parameter '%s' can be at most %" PRId64,
+                   name ? name : "null", max);
         return;
     }
 
#13 lore raw

PATCH v2 6/7 hw/intc/openpic: Improve errors for out of bounds property values

In reply to Markus Armbruster

The error message doesn't matter much, as the "openpic" device isn't user-creatable. But it's the last use of QERR_PROPERTY_VALUE_OUT_OF_RANGE, which has to go. Change the message just like the previous commit did for x86 CPUs.

---
 hw/intc/openpic.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
hw/intc/openpic.c +1 -4
index 32bd880dfa..cd3d87768e 100644
@@ -41,7 +41,6 @@
 #include "hw/pci/msi.h"
 #include "qapi/error.h"
 #include "qemu/bitops.h"
-#include "qapi/qmp/qerror.h"
 #include "qemu/module.h"
 #include "qemu/timer.h"
 #include "qemu/error-report.h"
@@ -1535,9 +1534,7 @@ static void openpic_realize(DeviceState *dev, Error **errp)
     };
 
     if (opp->nb_cpus > MAX_CPU) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
-                   TYPE_OPENPIC, "nb_cpus", (uint64_t)opp->nb_cpus,
-                   (uint64_t)0, (uint64_t)MAX_CPU);
+        error_setg(errp, "property 'nb_cpus' can be at most %d", MAX_CPU);
         return;
     }
 
Signature · 1 line
2.46.0
Reviewed-by on v2 6/7
#14 lore raw

Re: [PATCH v2 6/7] hw/intc/openpic: Improve errors for out of bounds property values

Quoted 23 lines · Markus Armbruster

The error message doesn't matter much, as the "openpic" device isn't user-creatable. But it's the last use of QERR_PROPERTY_VALUE_OUT_OF_RANGE, which has to go. Change the message just like the previous commit did for x86 CPUs.

---
  hw/intc/openpic.c | 5 +----
  1 file changed, 1 insertion(+), 4 deletions(-)
hw/intc/openpic.c +0 -1
index 32bd880dfa..cd3d87768e 100644
@@ -41,7 +41,6 @@
  #include "hw/pci/msi.h"
  #include "qapi/error.h"
  #include "qemu/bitops.h"
-#include "qapi/qmp/qerror.h"
  #include "qemu/module.h"
  #include "qemu/timer.h"
  #include "qemu/error-report.h"

hw/intc/openpic.c static void openpic_realize(DeviceState *dev, Error **errp) · line 1540

Quoted 10 lines
hw/intc/openpic.c +1 -3
@@ -1535,9 +1534,7 @@ static void openpic_realize(DeviceState *dev, Error **errp)
      };
 
      if (opp->nb_cpus > MAX_CPU) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
-                   TYPE_OPENPIC, "nb_cpus", (uint64_t)opp->nb_cpus,
-                   (uint64_t)0, (uint64_t)MAX_CPU);
+        error_setg(errp, "property 'nb_cpus' can be at most %d", MAX_CPU);
          return;
      }
 

As another cleanup we could convert MAX_CPU to unsigned.

#15 lore raw

Re: [PATCH v2 6/7] hw/intc/openpic: Improve errors for out of bounds property values

Quoted 26 lines · Philippe Mathieu-Daudé
Quoted 25 lines · Markus Armbruster

The error message doesn't matter much, as the "openpic" device isn't user-creatable. But it's the last use of QERR_PROPERTY_VALUE_OUT_OF_RANGE, which has to go. Change the message just like the previous commit did for x86 CPUs.

---
  hw/intc/openpic.c | 5 +----
  1 file changed, 1 insertion(+), 4 deletions(-)
hw/intc/openpic.c +0 -1
index 32bd880dfa..cd3d87768e 100644
@@ -41,7 +41,6 @@
 #include "hw/pci/msi.h"
 #include "qapi/error.h"
 #include "qemu/bitops.h"
-#include "qapi/qmp/qerror.h"
 #include "qemu/module.h"
 #include "qemu/timer.h"
 #include "qemu/error-report.h"
@@ -1535,9 +1534,7 @@ static void openpic_realize(DeviceState *dev, Error **errp)
     };
 
Quoted 10 lines

hw/intc/openpic.c static void openpic_realize(DeviceState *dev, Error **errp) · line 1540

Quoted 8 lines
hw/intc/openpic.c +1 -3
     if (opp->nb_cpus > MAX_CPU) {
-        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
-                   TYPE_OPENPIC, "nb_cpus", (uint64_t)opp->nb_cpus,
-                   (uint64_t)0, (uint64_t)MAX_CPU);
+        error_setg(errp, "property 'nb_cpus' can be at most %d", MAX_CPU);
         return;
     }
 

As another cleanup we could convert MAX_CPU to unsigned.

Existing uses are all fine as is. Perhaps the maintainer has a preference.

Quoted 1 line

Thanks!

#16 lore raw

PATCH v2 7/7 qerror: QERR_PROPERTY_VALUE_OUT_OF_RANGE is no longer used, drop

In reply to Markus Armbruster
---
 include/qapi/qmp/qerror.h | 3 ---
 1 file changed, 3 deletions(-)
include/qapi/qmp/qerror.h +0 -3
index 101c1141b9..d1db6f18cd 100644
@@ -23,7 +23,4 @@
 #define QERR_MISSING_PARAMETER \
     "Parameter '%s' is missing"
 
-#define QERR_PROPERTY_VALUE_OUT_OF_RANGE \
-    "Property %s.%s doesn't take value %" PRId64 " (minimum: %" PRId64 ", maximum: %" PRId64 ")"
-
 #endif /* QERROR_H */
Signature · 1 line
2.46.0