Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 23 Jan 2003 04:54:37 +0200
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        hackers@freebsd.org
Subject:   Checking sockaddr_in port number for overflow
Message-ID:  <20030123025437.GA40516@gothmog.gr>

next in thread | raw e-mail | index | archive | help
I have been trying to think of a good way to check for overflow of
port numbers of PF_INET sockets that are passed by the user. So far,
this is what I have come up with:

: #include <netinet/in.h>
:
: #include <err.h>
: #include <errno.h>
: #include <limits.h>
: #include <stdio.h>
: #include <stdlib.h>
:
: int
: main(int argc, char *argv[])
: {
:         static long      tmp;
:         static char     *errp;
:         in_port_t        port;
:
:         if (argc != 2)
:                 errx(1, "usage: %s port", argv[0]);
:
:         errno == 0;
:         tmp = strtol(argv[1], &errp, 0);
:         if (errp == argv[1] || errno == EINVAL || errno == ERANGE)
:                 errx(1, "invalid number '%s'", argv[1]);
:         if (tmp > IPPORT_MAX)
:                 errx(1, "%ld will overflow as a port number (max %d)",
:                     tmp, IPPORT_MAX);
:
:         port = (in_port_t)tmp;
:         printf("Using port number %d\n", port);
:         return (0);
: }

Does this look ok to you all?
Have I missed anything obvious that I should also check?

- Giorgos


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030123025437.GA40516>