mirror of
https://git.proxmox.com/git/mirror_corosync
synced 2025-08-24 11:38:51 +00:00

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>
36 lines
689 B
C
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);
|
|
}
|