Commit 4fcdf65a authored by Markus Armbruster's avatar Markus Armbruster
Browse files

util/cutils: Let qemu_strtosz*() optionally reject trailing crap



Change the qemu_strtosz() & friends to return -EINVAL when @endptr is
null and the conversion doesn't consume the string completely.
Matches how qemu_strtol() & friends work.

Only test_qemu_strtosz_simple() passes a null @endptr.  No functional
change there, because its conversion consumes the string.

Simplify callers that use @endptr only to fail when it doesn't point
to '\0' to pass a null @endptr instead.

Cc: Dr. David Alan Gilbert <dgilbert@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com> (maintainer:X86)
Cc: Kevin Wolf <kwolf@redhat.com> (supporter:Block layer core)
Cc: Max Reitz <mreitz@redhat.com> (supporter:Block layer core)
Cc: qemu-block@nongnu.org (open list:Block layer core)
Signed-off-by: default avatarMarkus Armbruster <armbru@redhat.com>
Reviewed-by: default avatarEric Blake <eblake@redhat.com>
Reviewed-by: default avatarDr. David Alan Gilbert <dgilbert@redhat.com>
Message-Id: <1487708048-2131-22-git-send-email-armbru@redhat.com>
parent 606caa0a
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -1346,7 +1346,6 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
    const char *valuestr = qdict_get_str(qdict, "value");
    int64_t valuebw = 0;
    long valueint = 0;
    char *endp;
    Error *err = NULL;
    bool use_int_value = false;
    int i;
@@ -1385,9 +1384,8 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
                break;
            case MIGRATION_PARAMETER_MAX_BANDWIDTH:
                p.has_max_bandwidth = true;
                valuebw = qemu_strtosz_MiB(valuestr, &endp);
                if (valuebw < 0 || (size_t)valuebw != valuebw
                    || *endp != '\0') {
                valuebw = qemu_strtosz_MiB(valuestr, NULL);
                if (valuebw < 0 || (size_t)valuebw != valuebw) {
                    error_setg(&err, "Invalid size %s", valuestr);
                    goto cleanup;
                }
+2 −4
Original line number Diff line number Diff line
@@ -1267,10 +1267,8 @@ static void ivshmem_realize(PCIDevice *dev, Error **errp)
    if (s->sizearg == NULL) {
        s->legacy_size = 4 << 20; /* 4 MB default */
    } else {
        char *end;
        int64_t size = qemu_strtosz_MiB(s->sizearg, &end);
        if (size < 0 || (size_t)size != size || *end != '\0'
            || !is_power_of_2(size)) {
        int64_t size = qemu_strtosz_MiB(s->sizearg, NULL);
        if (size < 0 || (size_t)size != size || !is_power_of_2(size)) {
            error_setg(errp, "Invalid size %s", s->sizearg);
            return;
        }
+2 −3
Original line number Diff line number Diff line
@@ -482,15 +482,14 @@ opts_type_size(Visitor *v, const char *name, uint64_t *obj, Error **errp)
    OptsVisitor *ov = to_ov(v);
    const QemuOpt *opt;
    int64_t val;
    char *endptr;

    opt = lookup_scalar(ov, name, errp);
    if (!opt) {
        return;
    }

    val = qemu_strtosz(opt->str ? opt->str : "", &endptr);
    if (val < 0 || *endptr) {
    val = qemu_strtosz(opt->str ? opt->str : "", NULL);
    if (val < 0) {
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
                   "a size value representible as a non-negative int64");
        return;
+1 −6
Original line number Diff line number Diff line
@@ -370,14 +370,9 @@ static int add_old_style_options(const char *fmt, QemuOpts *opts,

static int64_t cvtnum(const char *s)
{
    char *end;
    int64_t ret;

    ret = qemu_strtosz(s, &end);
    if (*end != '\0') {
        /* Detritus at the end of the string */
        return -EINVAL;
    }
    ret = qemu_strtosz(s, NULL);
    return ret;
}

+1 −6
Original line number Diff line number Diff line
@@ -137,14 +137,9 @@ static char **breakline(char *input, int *count)

static int64_t cvtnum(const char *s)
{
    char *end;
    int64_t ret;

    ret = qemu_strtosz(s, &end);
    if (*end != '\0') {
        /* Detritus at the end of the string */
        return -EINVAL;
    }
    ret = qemu_strtosz(s, NULL);
    return ret;
}

Loading