Skip to content

Commit ba5b07f

Browse files
committed
data parser BUGFIX negative values of unsigned integers
strtoull() handles negative values as valid input for unsigned integers and converts them to equivalent unsigned values (UMAX - abs_value). For YANG values this is an error and negative value is invalid. Fixes #345
1 parent f2a79aa commit ba5b07f

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/parser.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -764,44 +764,39 @@ static int
764764
parse_uint(const char *val_str, uint64_t max, int base, uint64_t *ret, struct lyd_node *node)
765765
{
766766
char *strptr;
767+
uint64_t u;
767768

768769
if (!val_str || !val_str[0]) {
769-
if (node) {
770-
LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, "", node->schema->name);
771-
} else {
772-
ly_errno = LY_EVALID;
773-
ly_vecode = LYVE_INVAL;
774-
}
775-
return EXIT_FAILURE;
770+
goto error;
776771
}
777772

778773
errno = 0;
779774
strptr = NULL;
780-
*ret = strtoull(val_str, &strptr, base);
781-
if (errno || (*ret > max)) {
782-
if (node) {
783-
LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, val_str, node->schema->name);
784-
} else {
785-
ly_errno = LY_EVALID;
786-
ly_vecode = LYVE_INVAL;
787-
}
788-
return EXIT_FAILURE;
775+
u = strtoull(val_str, &strptr, base);
776+
if (errno || (u > max)) {
777+
goto error;
789778
} else if (strptr && *strptr) {
790779
while (isspace(*strptr)) {
791780
++strptr;
792781
}
793782
if (*strptr) {
794-
if (node) {
795-
LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, val_str, node->schema->name);
796-
} else {
797-
ly_errno = LY_EVALID;
798-
ly_vecode = LYVE_INVAL;
799-
}
800-
return EXIT_FAILURE;
783+
goto error;
801784
}
785+
} else if (u != 0 && val_str[0] == '-') {
786+
goto error;
802787
}
803788

789+
*ret = u;
804790
return EXIT_SUCCESS;
791+
792+
error:
793+
if (node) {
794+
LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, val_str ? val_str : "", node->schema->name);
795+
} else {
796+
ly_errno = LY_EVALID;
797+
ly_vecode = LYVE_INVAL;
798+
}
799+
return EXIT_FAILURE;
805800
}
806801

807802
/* logs directly

0 commit comments

Comments
 (0)