mirror_corosync/tools/util.c
liangxin1300 fb5e0fae92 tools: use util_strtonum for options checking
Function atoi is not safe since miss validation;
Function strtol is better but need to consider empty string and overflows
Function util_strtonum is a safer wrapper of strtoll

Use util_strtonum to check nodeid option and strict checking condition.

Signed-off-by: liangxin1300 <XLiang@suse.com>
Reviewed-by: Jan Friesse <jfriesse@redhat.com>
2020-08-12 16:56:34 +02:00

36 lines
689 B
C

#include <stdlib.h>
#include <errno.h>
#include "util.h"
/*
* Safer wrapper of strtoll. Return 0 on success, otherwise -1.
* Idea from corosync-qdevice project
*/
int
util_strtonum(const char *str, long long int min_val, long long int max_val,
long long int *res)
{
long long int tmp_ll;
char *ep;
if (min_val > max_val) {
return (-1);
}
errno = 0;
tmp_ll = strtoll(str, &ep, 10);
if (ep == str || *ep != '\0' || errno != 0) {
return (-1);
}
if (tmp_ll < min_val || tmp_ll > max_val) {
return (-1);
}
*res = tmp_ll;
return (0);
}