1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

numbers: strtod or strtoul need reset of errno

API for strtod() or strtoul() needs reset of errno, before it's being
called. So add missing resets in missing places and some also some
errno validation for out-of-range numbers.
This commit is contained in:
Zdenek Kabelac 2017-07-16 10:28:02 +02:00
parent 94838b4df0
commit ba9820b142
8 changed files with 44 additions and 12 deletions

View File

@ -225,8 +225,9 @@ static int _is_open(const char *path)
while ((proc_dp = readdir(proc_d)) != NULL) { while ((proc_dp = readdir(proc_d)) != NULL) {
if (!isdigit(proc_dp->d_name[0])) if (!isdigit(proc_dp->d_name[0]))
continue; continue;
errno = 0;
pid = (pid_t) strtol(proc_dp->d_name, NULL, 10); pid = (pid_t) strtol(proc_dp->d_name, NULL, 10);
if (!pid) if (errno || !pid)
continue; continue;
if (_is_open_in_pid(pid, path)) { if (_is_open_in_pid(pid, path)) {
if (closedir(proc_d)) if (closedir(proc_d))
@ -278,8 +279,9 @@ static int _parse_args(int argc, char **argv, struct filemap_monitor *fm)
fm->nr_regions = 1; fm->nr_regions = 1;
/* parse <fd> */ /* parse <fd> */
errno = 0;
fm->fd = (int) strtol(argv[0], &endptr, 10); fm->fd = (int) strtol(argv[0], &endptr, 10);
if (*endptr) { if (errno || *endptr) {
_early_log("Could not parse file descriptor: %s", argv[0]); _early_log("Could not parse file descriptor: %s", argv[0]);
return 0; return 0;
} }
@ -288,8 +290,9 @@ static int _parse_args(int argc, char **argv, struct filemap_monitor *fm)
argv++; argv++;
/* parse <group_id> */ /* parse <group_id> */
errno = 0;
fm->group_id = strtoull(argv[0], &endptr, 10); fm->group_id = strtoull(argv[0], &endptr, 10);
if (*endptr) { if (*endptr || errno) {
_early_log("Could not parse group identifier: %s", argv[0]); _early_log("Could not parse group identifier: %s", argv[0]);
return 0; return 0;
} }
@ -332,8 +335,9 @@ static int _parse_args(int argc, char **argv, struct filemap_monitor *fm)
/* parse [<foreground>[<verbose>]] */ /* parse [<foreground>[<verbose>]] */
if (argc) { if (argc) {
errno = 0;
_foreground = (int) strtol(argv[0], &endptr, 10); _foreground = (int) strtol(argv[0], &endptr, 10);
if (*endptr) { if (errno || *endptr) {
_early_log("Could not parse debug argument: %s.", _early_log("Could not parse debug argument: %s.",
argv[0]); argv[0]);
return 0; return 0;
@ -341,8 +345,9 @@ static int _parse_args(int argc, char **argv, struct filemap_monitor *fm)
argc--; argc--;
argv++; argv++;
if (argc) { if (argc) {
errno = 0;
_verbose = (int) strtol(argv[0], &endptr, 10); _verbose = (int) strtol(argv[0], &endptr, 10);
if (*endptr) { if (errno || *endptr) {
_early_log("Could not parse verbose " _early_log("Could not parse verbose "
"argument: %s", argv[0]); "argument: %s", argv[0]);
return 0; return 0;

View File

@ -224,7 +224,10 @@ static int lock_lv_offset_from_args(char *lv_args, uint64_t *lock_lv_offset)
if (rv < 0) if (rv < 0)
return rv; return rv;
errno = 0;
*lock_lv_offset = strtoull(offset_str, NULL, 10); *lock_lv_offset = strtoull(offset_str, NULL, 10);
if (errno)
return -1;
return 0; return 0;
} }

View File

@ -742,14 +742,16 @@ static struct dm_config_value *_get_def_array_values(struct cmd_context *cmd,
switch (toupper(token[0])) { switch (toupper(token[0])) {
case 'I': case 'I':
case 'B': case 'B':
errno = 0;
v->v.i = strtoll(token + 1, &r, 10); v->v.i = strtoll(token + 1, &r, 10);
if (*r) if (errno || *r)
goto bad; goto bad;
v->type = DM_CFG_INT; v->type = DM_CFG_INT;
break; break;
case 'F': case 'F':
errno = 0;
v->v.f = strtod(token + 1, &r); v->v.f = strtod(token + 1, &r);
if (*r) if (errno || *r)
goto bad; goto bad;
v->type = DM_CFG_FLOAT; v->type = DM_CFG_FLOAT;
break; break;

View File

@ -679,13 +679,23 @@ static struct dm_config_value *_type(struct parser *p)
switch (p->t) { switch (p->t) {
case TOK_INT: case TOK_INT:
v->type = DM_CFG_INT; v->type = DM_CFG_INT;
errno = 0;
v->v.i = strtoll(p->tb, NULL, 0); /* FIXME: check error */ v->v.i = strtoll(p->tb, NULL, 0); /* FIXME: check error */
if (errno) {
log_error("Failed to read int token.");
return NULL;
}
match(TOK_INT); match(TOK_INT);
break; break;
case TOK_FLOAT: case TOK_FLOAT:
v->type = DM_CFG_FLOAT; v->type = DM_CFG_FLOAT;
errno = 0;
v->v.f = strtod(p->tb, NULL); /* FIXME: check error */ v->v.f = strtod(p->tb, NULL); /* FIXME: check error */
if (errno) {
log_error("Failed to read float token.");
return NULL;
}
match(TOK_FLOAT); match(TOK_FLOAT);
break; break;

View File

@ -3185,6 +3185,7 @@ static const char *_tok_value_time(const struct dm_report_field_type *ft,
goto out; goto out;
} }
errno = 0;
if (((t = strtoull(time_str, NULL, 10)) == ULLONG_MAX) && errno == ERANGE) { if (((t = strtoull(time_str, NULL, 10)) == ULLONG_MAX) && errno == ERANGE) {
log_error(_out_of_range_msg, time_str, ft->id); log_error(_out_of_range_msg, time_str, ft->id);
goto out; goto out;
@ -3555,6 +3556,7 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
fs->value->next->v.i = (((const uint64_t *) rvw->value)[1]); fs->value->next->v.i = (((const uint64_t *) rvw->value)[1]);
} else { } else {
errno = 0;
if (((fs->value->v.i = strtoull(s, NULL, 10)) == ULLONG_MAX) && if (((fs->value->v.i = strtoull(s, NULL, 10)) == ULLONG_MAX) &&
(errno == ERANGE)) { (errno == ERANGE)) {
log_error(_out_of_range_msg, s, field_id); log_error(_out_of_range_msg, s, field_id);
@ -3573,6 +3575,7 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
fs->value->next->v.d = (((const double *) rvw->value)[1]); fs->value->next->v.d = (((const double *) rvw->value)[1]);
} else { } else {
errno = 0;
fs->value->v.d = strtod(s, NULL); fs->value->v.d = strtod(s, NULL);
if (errno == ERANGE) { if (errno == ERANGE) {
log_error(_out_of_range_msg, s, field_id); log_error(_out_of_range_msg, s, field_id);
@ -3594,6 +3597,7 @@ static struct field_selection *_create_field_selection(struct dm_report *rh,
if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
fs->value->next->v.i = (((const uint64_t *) rvw->value)[1]); fs->value->next->v.i = (((const uint64_t *) rvw->value)[1]);
} else { } else {
errno = 0;
fs->value->v.d = strtod(s, NULL); fs->value->v.d = strtod(s, NULL);
if ((errno == ERANGE) || (fs->value->v.d < 0) || (fs->value->v.d > 100)) { if ((errno == ERANGE) || (fs->value->v.d < 0) || (fs->value->v.d > 100)) {
log_error(_out_of_range_msg, s, field_id); log_error(_out_of_range_msg, s, field_id);

View File

@ -853,8 +853,9 @@ static int _stats_parse_histogram_spec(struct dm_stats *dms,
val_start = c; val_start = c;
endptr = NULL; endptr = NULL;
errno = 0;
this_val = strtoull(val_start, &endptr, 10); this_val = strtoull(val_start, &endptr, 10);
if (!endptr) { if (errno || !endptr) {
log_error("Could not parse histogram boundary."); log_error("Could not parse histogram boundary.");
goto bad; goto bad;
} }
@ -1174,8 +1175,9 @@ static int _stats_parse_histogram(struct dm_pool *mem, char *hist_str,
val_start = c; val_start = c;
endptr = NULL; endptr = NULL;
errno = 0;
this_val = strtoull(val_start, &endptr, 10); this_val = strtoull(val_start, &endptr, 10);
if (!endptr) { if (errno || !endptr) {
log_error("Could not parse histogram value."); log_error("Could not parse histogram value.");
goto bad; goto bad;
} }
@ -1992,8 +1994,9 @@ static int _stats_create_region(struct dm_stats *dms, uint64_t *region_id,
} }
if (region_id) { if (region_id) {
errno = 0;
*region_id = strtoull(resp, &endptr, 10); *region_id = strtoull(resp, &endptr, 10);
if (resp == endptr) if (errno || resp == endptr)
goto_out; goto_out;
} }

View File

@ -1270,8 +1270,9 @@ static int _message(CMD_ARGS)
argv++; argv++;
} }
errno = 0;
sector = strtoull(argv[0], &endptr, 10); sector = strtoull(argv[0], &endptr, 10);
if (*endptr || endptr == argv[0]) { if (errno || *endptr || endptr == argv[0]) {
err("invalid sector"); err("invalid sector");
goto out; goto out;
} }

View File

@ -563,6 +563,7 @@ static int _size_arg(struct cmd_context *cmd __attribute__((unused)),
if (!isdigit(*val)) if (!isdigit(*val))
return 0; return 0;
errno = 0;
v = strtod(val, &ptr); v = strtod(val, &ptr);
if (*ptr == '.') { if (*ptr == '.') {
@ -571,13 +572,16 @@ static int _size_arg(struct cmd_context *cmd __attribute__((unused)),
* Lets be toleran and retry with standard C locales * Lets be toleran and retry with standard C locales
*/ */
if (setlocale(LC_ALL, "C")) { if (setlocale(LC_ALL, "C")) {
errno = 0;
v = strtod(val, &ptr); v = strtod(val, &ptr);
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
} }
} }
if (ptr == val) if (ptr == val || errno) {
log_error("Can't parse size argument at '%c'.%s%s", ptr[0], (errno) ? " " :"", (errno) ? strerror(errno) : "");
return 0; return 0;
}
if (percent && *ptr == '%') { if (percent && *ptr == '%') {
if (!_get_percent_arg(av, ++ptr)) if (!_get_percent_arg(av, ++ptr))