err = qemu_strtol(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtol_whitespace(void)
err = qemu_strtol(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtol_invalid(void)
err = qemu_strtol(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtol_trailing(void)
err = qemu_strtol(str, NULL, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtol_full_negative(void)
err = qemu_strtoul(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoul_whitespace(void)
err = qemu_strtoul(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoul_invalid(void)
err = qemu_strtoul(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoul_trailing(void)
err = qemu_strtoul(str, NULL, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoul_full_negative(void)
{
err = qemu_strtoll(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoll_whitespace(void)
err = qemu_strtoll(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoll_invalid(void)
err = qemu_strtoll(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoll_trailing(void)
err = qemu_strtoll(str, NULL, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoll_full_negative(void)
err = qemu_strtoull(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoull_whitespace(void)
err = qemu_strtoull(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoull_invalid(void)
err = qemu_strtoull(str, &endptr, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert(endptr == str);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoull_trailing(void)
err = qemu_strtoull(str, NULL, 0, &res);
- g_assert_cmpint(err, ==, 0);
- g_assert_cmpint(res, ==, 0);
+ g_assert_cmpint(err, ==, -EINVAL);
}
static void test_qemu_strtoull_full_negative(void)
/**
* Helper function for qemu_strto*l() functions.
*/
-static int check_strtox_error(const char **next, char *endptr,
+static int check_strtox_error(const char *p, char *endptr, const char **next,
int err)
{
+ /* If no conversion was performed, prefer BSD behavior over glibc
+ * behavior.
+ */
+ if (err == 0 && endptr == p) {
+ err = EINVAL;
+ }
if (!next && *endptr) {
return -EINVAL;
}
} else {
errno = 0;
*result = strtol(nptr, &p, base);
- err = check_strtox_error(endptr, p, errno);
+ err = check_strtox_error(nptr, p, endptr, errno);
}
return err;
}
} else {
errno = 0;
*result = strtoul(nptr, &p, base);
- err = check_strtox_error(endptr, p, errno);
+ /* Windows returns 1 for negative out-of-range values. */
+ if (errno == ERANGE) {
+ *result = -1;
+ }
+ err = check_strtox_error(nptr, p, endptr, errno);
}
return err;
}
} else {
errno = 0;
*result = strtoll(nptr, &p, base);
- err = check_strtox_error(endptr, p, errno);
+ err = check_strtox_error(nptr, p, endptr, errno);
}
return err;
}
} else {
errno = 0;
*result = strtoull(nptr, &p, base);
- err = check_strtox_error(endptr, p, errno);
+ /* Windows returns 1 for negative out-of-range values. */
+ if (errno == ERANGE) {
+ *result = -1;
+ }
+ err = check_strtox_error(nptr, p, endptr, errno);
}
return err;
}