]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Check correct lengths and use closefrom where available.
authorRoy Marples <roy@marples.name>
Thu, 15 May 2008 16:09:42 +0000 (16:09 +0000)
committerRoy Marples <roy@marples.name>
Thu, 15 May 2008 16:09:42 +0000 (16:09 +0000)
common.c
common.h
dhcpcd.c

index 16ef01b109cda756b13e4d6fcbcbf244b97885a9..cad633eef57569154ea426e546452e17e26c7fa5 100644 (file)
--- a/common.c
+++ b/common.c
@@ -109,6 +109,20 @@ strlcpy(char *dst, const char *src, size_t size)
 }
 #endif
 
+#ifndef HAVE_CLOSEFROM
+int
+closefrom(int fd)
+{
+       int max = getdtablesize();
+       int i;
+       int r = 0;
+
+       for (i = fd; i < max; i++)
+               r += close(i);
+       return r;
+}
+#endif
+
 /* Close our fd's */
 int
 close_fds(void)
index 8689cf1ed52224eaa6b221c2ae812e4a6123fa16..90d84fe9db011e5ccf770bad7850e67082288202 100644 (file)
--- a/common.h
+++ b/common.h
@@ -55,6 +55,12 @@ size_t strlcpy(char *, const char *, size_t);
 void srandomdev(void);
 #endif
 
+#define HAVE_CLOSEFROM
+#if defined(__linux__) || defined(__FreeBSD__)
+#  undef HAVE_CLOSEFROM
+int closefrom(int);
+#endif
+
 int close_fds(void);
 int close_on_exec(int);
 ssize_t get_line(char **, size_t *, FILE *);
index 71005657c5772039636f450369b2fffaa81ca9f5..435c99b6295cc29001d0a938714b8e6f9b9e339b 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -171,7 +171,7 @@ parse_option(int opt, char *oarg, struct options *options)
        case 'h':
                if (!oarg)
                        *options->hostname = '\0';
-               else if (strlen(oarg) > MAXHOSTNAMELEN) {
+               else if (strlen(oarg) >= MAXHOSTNAMELEN) {
                        logger(LOG_ERR,
                               "`%s' too long for HostName string, max is %d",
                               oarg, MAXHOSTNAMELEN);
@@ -183,7 +183,7 @@ parse_option(int opt, char *oarg, struct options *options)
        case 'i':
                if (!oarg) {
                        *options->classid = '\0';
-               } else if (strlen(oarg) > CLASS_ID_MAX_LEN) {
+               } else if (strlen(oarg) >= CLASS_ID_MAX_LEN) {
                        logger(LOG_ERR,
                               "`%s' too long for ClassID string, max is %d",
                               oarg, CLASS_ID_MAX_LEN);
@@ -265,7 +265,7 @@ parse_option(int opt, char *oarg, struct options *options)
                j = 0;
                for (i = 0; i < userclasses; i++)
                        j += (int)options->userclass[j] + 1;
-               if (j + 1 + strlen(oarg) > USERCLASS_MAX_LEN) {
+               if (j + 1 + strlen(oarg) >= USERCLASS_MAX_LEN) {
                        logger(LOG_ERR,
                               "userclass overrun, max is %d",
                               USERCLASS_MAX_LEN);
@@ -310,7 +310,7 @@ parse_option(int opt, char *oarg, struct options *options)
                break;
        case 'I':
                if (oarg) {
-                       if (strlen(oarg) > CLIENT_ID_MAX_LEN) {
+                       if (strlen(oarg) >= CLIENT_ID_MAX_LEN) {
                                logger(LOG_ERR, "`%s' is too long for"
                                       " ClientID, max is %d",
                                       oarg, CLIENT_ID_MAX_LEN);
@@ -382,10 +382,7 @@ main(int argc, char **argv)
        char *cf = NULL;
        char *intf = NULL;
 
-       /* Close any un-needed fd's */
-       for (i = getdtablesize() - 1; i >= 3; --i)
-               close (i);
-
+       closefrom(3);
        openlog(PACKAGE, LOG_PID, LOG_LOCAL0);
 
 #ifdef THERE_IS_NO_FORK
@@ -480,7 +477,7 @@ main(int argc, char **argv)
                usage();
 
        if (optind < argc) {
-               if (strlen(argv[optind]) > IF_NAMESIZE) {
+               if (strlen(argv[optind]) >= IF_NAMESIZE) {
                        logger(LOG_ERR,
                               "`%s' too long for an interface name (max=%d)",
                               argv[optind], IF_NAMESIZE);