Skip to content

Commit 019a5f4

Browse files
deraadt@openbsd.orgdjmdjm
authored andcommitted
upstream: Use strtonum() instead of severely non-idomatic
strtoul() In particular this will now reject trailing garbage, ie. '12garbage'. ok djm OpenBSD-Commit-ID: c82d95e3ccbfedfc91a8041c2f8bf0cf987d1501
1 parent 8231ca0 commit 019a5f4

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

addr.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: addr.c,v 1.7 2023/03/27 03:31:05 djm Exp $ */
1+
/* $OpenBSD: addr.c,v 1.8 2024/04/02 09:29:31 deraadt Exp $ */
22

33
/*
44
* Copyright (c) 2004-2008 Damien Miller <[email protected]>
@@ -27,6 +27,7 @@
2727
#include <string.h>
2828
#include <stdlib.h>
2929
#include <stdio.h>
30+
#include <limits.h>
3031

3132
#include "addr.h"
3233

@@ -457,8 +458,9 @@ int
457458
addr_pton_cidr(const char *p, struct xaddr *n, u_int *l)
458459
{
459460
struct xaddr tmp;
460-
long unsigned int masklen = 999;
461-
char addrbuf[64], *mp, *cp;
461+
u_int masklen = 999;
462+
char addrbuf[64], *mp;
463+
const char *errstr;
462464

463465
/* Don't modify argument */
464466
if (p == NULL || strlcpy(addrbuf, p, sizeof(addrbuf)) >= sizeof(addrbuf))
@@ -467,8 +469,8 @@ addr_pton_cidr(const char *p, struct xaddr *n, u_int *l)
467469
if ((mp = strchr(addrbuf, '/')) != NULL) {
468470
*mp = '\0';
469471
mp++;
470-
masklen = strtoul(mp, &cp, 10);
471-
if (*mp < '0' || *mp > '9' || *cp != '\0' || masklen > 128)
472+
masklen = (u_int)strtonum(mp, 0, INT_MAX, &errstr);
473+
if (errstr)
472474
return -1;
473475
}
474476

0 commit comments

Comments
 (0)