Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/date - check return of strtoll



details:   https://anonhg.NetBSD.org/src/rev/89d072e7cc59
branches:  trunk
changeset: 759483:89d072e7cc59
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Dec 11 16:57:51 2010 +0000

description:
- check return of strtoll
- misc cleanup

diffstat:

 bin/date/date.c    |  32 +++++++++++++++++++++-----------
 bin/date/netdate.c |  37 +++++++++++++++++--------------------
 2 files changed, 38 insertions(+), 31 deletions(-)

diffs (242 lines):

diff -r 49cd3921dfce -r 89d072e7cc59 bin/date/date.c
--- a/bin/date/date.c   Sat Dec 11 15:49:34 2010 +0000
+++ b/bin/date/date.c   Sat Dec 11 16:57:51 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: date.c,v 1.55 2010/10/03 08:21:27 gson Exp $ */
+/* $NetBSD: date.c,v 1.56 2010/12/11 16:57:51 christos Exp $ */
 
 /*
  * Copyright (c) 1985, 1987, 1988, 1993
@@ -40,7 +40,7 @@
 #if 0
 static char sccsid[] = "@(#)date.c     8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: date.c,v 1.55 2010/10/03 08:21:27 gson Exp $");
+__RCSID("$NetBSD: date.c,v 1.56 2010/12/11 16:57:51 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -50,6 +50,7 @@
 #include <ctype.h>
 #include <err.h>
 #include <fcntl.h>
+#include <errno.h>
 #include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -64,13 +65,12 @@
 
 static time_t tval;
 static int aflag, jflag, rflag, nflag;
-int retval;
 
 static void badformat(void);
 static void badtime(void);
 static void badvalue(const char *);
 static void setthetime(const char *);
-static void usage(void);
+static void usage(void) __attribute__((__noreturn__));
 
 int
 main(int argc, char *argv[])
@@ -79,6 +79,7 @@
        size_t bufsiz;
        const char *format;
        int ch;
+       long long val;
 
        setprogname(argv[0]);
        (void)setlocale(LC_ALL, "");
@@ -92,8 +93,9 @@
                case 'd':
                        rflag = 1;
                        tval = parsedate(optarg, NULL, NULL);
-                       if (tval == -1)
-                               errx(1, "Cannot parse `%s'", optarg);
+                       if (tval == -1) 
+badarg:                                 errx(EXIT_FAILURE,
+                                   "Cannot parse `%s'", optarg);
                        break;
                case 'j':               /* don't set time */
                        jflag = 1;
@@ -102,8 +104,15 @@
                        nflag = 1;
                        break;
                case 'r':               /* user specified seconds */
+                       errno = 0;
+                       val = strtoll(optarg, &buf, 0);
+                       if (optarg[0] == '\0' || *buf != '\0')
+                               goto badarg;
+                       if (errno == ERANGE && (val == LLONG_MAX ||
+                           val == LLONG_MIN))
+                               err(EXIT_FAILURE, "Bad number `%s'", optarg);
                        rflag = 1;
-                       tval = strtoll(optarg, NULL, 0);
+                       tval = (time_t)val;
                        break;
                case 'u':               /* do everything in UTC */
                        (void)setenv("TZ", "UTC0", 1);
@@ -118,13 +127,13 @@
        if (!rflag && time(&tval) == -1)
                err(EXIT_FAILURE, "time");
 
-       format = "+%a %b %e %H:%M:%S %Z %Y";
 
        /* allow the operands in any order */
        if (*argv && **argv == '+') {
                format = *argv;
                ++argv;
-       }
+       } else
+               format = "+%a %b %e %H:%M:%S %Z %Y";
 
        if (*argv) {
                setthetime(*argv);
@@ -143,7 +152,7 @@
        free(buf);
        return 0;
 bad:
-       err(1, "Cannot allocate format buffer");
+       err(EXIT_FAILURE, "Cannot allocate format buffer");
 }
 
 static void
@@ -317,7 +326,8 @@
 usage(void)
 {
        (void)fprintf(stderr,
-           "usage: %s [-ajnu] [-d date] [-r seconds] [+format]", getprogname());
+           "Usage: %s [-ajnu] [-d date] [-r seconds] [+format]",
+           getprogname());
        (void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
        exit(EXIT_FAILURE);
        /* NOTREACHED */
diff -r 49cd3921dfce -r 89d072e7cc59 bin/date/netdate.c
--- a/bin/date/netdate.c        Sat Dec 11 15:49:34 2010 +0000
+++ b/bin/date/netdate.c        Sat Dec 11 16:57:51 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: netdate.c,v 1.27 2008/02/24 04:49:45 dholland Exp $ */
+/* $NetBSD: netdate.c,v 1.28 2010/12/11 16:57:51 christos Exp $ */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)netdate.c  8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: netdate.c,v 1.27 2008/02/24 04:49:45 dholland Exp $");
+__RCSID("$NetBSD: netdate.c,v 1.28 2010/12/11 16:57:51 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -59,8 +59,6 @@
 #define        WAITACK         2000    /* milliseconds */
 #define        WAITDATEACK     5000    /* milliseconds */
 
-extern int retval;
-
 static const char *
 tsp_type_to_string(const struct tsp *msg)
 {
@@ -75,7 +73,7 @@
  * new date to the local timedaemon.  If the timedaemon is in the master state,
  * it performs the correction on all slaves.  If it is in the slave state, it
  * notifies the master that a correction is needed.
- * Returns 0 on success.  Returns > 0 on failure, setting retval to 2;
+ * Returns 0 on success.  Returns > 0 on failure.
  */
 int
 netsettime(time_t tval)
@@ -89,7 +87,7 @@
 
        if ((sp = getservbyname("timed", "udp")) == NULL) {
                warnx("udp/timed: unknown service");
-               return (retval = 2);
+               return 2;
        }
 
        (void)memset(&dest, 0, sizeof(dest));
@@ -100,18 +98,18 @@
        dest.sin_port = sp->s_port;
        dest.sin_addr.s_addr = htonl(INADDR_ANY);
        s = socket(AF_INET, SOCK_DGRAM, 0);
-       if (s < 0) {
+       if (s == -1) {
                if (errno != EAFNOSUPPORT)
                        warn("timed");
-               return (retval = 2);
+               return 2;
        }
 
 #ifdef IP_PORTRANGE
        {
                static const int on = IP_PORTRANGE_LOW;
 
-               if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE,
-                           &on, sizeof(on)) < 0) {
+               if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &on,
+                   sizeof(on)) == -1) {
                        warn("setsockopt");
                        goto bad;
                }
@@ -120,20 +118,19 @@
 
        msg.tsp_type = TSP_SETDATE;
        msg.tsp_vers = TSPVERSION;
-       if (gethostname(hostname, sizeof(hostname))) {
+       if (gethostname(hostname, sizeof(hostname)) == -1) {
                warn("gethostname");
                goto bad;
        }
-       strncpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
-       msg.tsp_name[sizeof(msg.tsp_name) - 1] = '\0';
+       (void)strlcpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
        msg.tsp_seq = htons((uint16_t)0);
        msg.tsp_time.tv_sec = htonl((uint32_t)tval); /* XXX: y2038 */
        msg.tsp_time.tv_usec = htonl((uint32_t)0);
-       if (connect(s, (const struct sockaddr *)&dest, sizeof(dest)) < 0) {
+       if (connect(s, (const void *)&dest, sizeof(dest)) == -1) {
                warn("connect");
                goto bad;
        }
-       if (send(s, &msg, sizeof(msg), 0) < 0) {
+       if (send(s, &msg, sizeof(msg), 0) == -1) {
                if (errno != ECONNREFUSED)
                        warn("send");
                goto bad;
@@ -151,7 +148,7 @@
                int error;
 
                length = sizeof(error);
-               if (!getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &length)
+               if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &length) == -1
                    && error) {
                        if (error != ECONNREFUSED)
                                warn("send (delayed error)");
@@ -162,8 +159,8 @@
        if (found > 0 && ready.revents & POLLIN) {
                ssize_t ret;
 
-               ret = recv(s, &msg, sizeof(msg), 0);
-               if (ret < 0) {
+               if ((ret = recv(s, &msg, sizeof(msg), 0)) == -1) {
+               if (ret < 0)
                        if (errno != ECONNREFUSED)
                                warn("recv");
                        goto bad;
@@ -182,7 +179,7 @@
                        goto loop;
                case TSP_DATEACK:
                        (void)close(s);
-                       return (0);
+                       return 0;
                default:
                        warnx("wrong ack received from timed: %s", 
                            tsp_type_to_string(&msg));
@@ -195,5 +192,5 @@
 
 bad:
        (void)close(s);
-       return (retval = 2);
+       return 2;
 }



Home | Main Index | Thread Index | Old Index