[PATCH] diagnose invalid host and port command line arguments; don't modify ARGV
Jim Meyering
jim at meyering.net
Mon Aug 8 13:31:45 UTC 2011
Jim Meyering wrote:
> There was inadequate verification of command line HOST:PORT (-d and -m)
> and -p PORT arguments and some duplicate code. The patch below factors
> out the duplication and adds checks to catch/diagnose invalid inputs.
> Along the way, it also resolves BZ 728623.
>
>>From 9dbd0d54065531654b42224931f4abb2a6685374 Mon Sep 17 00:00:00 2001
> From: Jim Meyering <meyering at redhat.com>
> Date: Mon, 8 Aug 2011 11:57:38 +0200
> Subject: [PATCH] diagnose invalid host and port command line arguments; don't
> modify ARGV
>
> * rest.c (get_port, extract_host_port): New functions.
> (main): Use them to parse arguments to the -d, -m and -p options.
> Before, a missing host name part and/or an invalid port number
> would not have been diagnosed.
> By no longer modifying "optarg", this address
> http://bugzilla.redhat.com/728623
> ---
> rest.c | 52 +++++++++++++++++++++++++++++++++++++++-------------
> 1 files changed, 39 insertions(+), 13 deletions(-)
>
> diff --git a/rest.c b/rest.c
> index a54fabd..6125275 100644
> --- a/rest.c
> +++ b/rest.c
> @@ -51,6 +51,8 @@
> #include "mpipe.h"
> #include "state_defs.h"
> #include "version-etc.h"
> +#include "xstrndup.h"
> +#include "xstrtol.h"
Not sure if that patch was at fault or some subsequent addition,
but there were some type mismatches, which led to these additions:
(just folded into the patch above)
diff --git a/gc-wrap.h b/gc-wrap.h
index f337e62..cba2f57 100644
--- a/gc-wrap.h
+++ b/gc-wrap.h
@@ -11,6 +11,8 @@
# define realloc(p,n) GC_REALLOC((p),(n))
#endif
+extern void xalloc_die (void) __attribute__ ((__noreturn__));
+
static inline char *
my_strdup (char const *s)
{
@@ -35,3 +37,33 @@ my_strndup (char const *s, size_t n)
}
# undef strndup
# define strndup(s, n) my_strndup(s, n)
+
+static void *
+xmalloc (size_t n)
+{
+ void *p = malloc (n);
+ if (!p && n != 0)
+ xalloc_die ();
+ return p;
+}
+
+static inline char *
+xstrndup (const char *string, size_t n)
+{
+ char *s = strndup (string, n);
+ if (! s)
+ xalloc_die ();
+ return s;
+}
+
+static void *
+xmemdup (void const *p, size_t s)
+{
+ return memcpy (xmalloc (s), p, s);
+}
+
+static inline char *
+xstrdup (char const *string)
+{
+ return (char *) xmemdup (string, strlen (string) + 1);
+}
diff --git a/iwh.h b/iwh.h
index 9351771..e18a7a0 100644
--- a/iwh.h
+++ b/iwh.h
@@ -22,9 +22,9 @@
#endif
GLOBAL(int, verbose, 0);
-GLOBAL(const char *, master_host, NULL);
+GLOBAL(char *, master_host, NULL);
GLOBAL(unsigned short, master_port, MY_PORT);
-GLOBAL(const char *, db_host, "localhost");
+GLOBAL(char *, db_host, NULL);
GLOBAL(unsigned short, db_port, 0);
GLOBAL(const char *, me, "here");
diff --git a/rest.c b/rest.c
index 6125275..7bf8fe1 100644
--- a/rest.c
+++ b/rest.c
@@ -51,7 +51,6 @@
#include "mpipe.h"
#include "state_defs.h"
#include "version-etc.h"
-#include "xstrndup.h"
#include "xstrtol.h"
const char version_etc_copyright[] =
@@ -2392,7 +2391,7 @@ get_port (char const *port_str)
port number. Diagnose and exit nonzero if the hostname part is empty
or if the port string is not a valid unsigned integer <= SHRT_MAX. */
static void
-extract_host_port (char const *s, const char **hostname, unsigned short *port)
+extract_host_port (char const *s, char **hostname, unsigned short *port)
{
assert (s);
size_t s_len = strlen (s);
@@ -2422,6 +2421,7 @@ main (int argc, char **argv)
atexit (close_stdout);
GC_INIT ();
+ db_host = xstrdup ("localhost");
for (;;)
switch (getopt_long(argc,argv,"ac:d:l:m:p:v",my_options,NULL)) {
More information about the iwhd-devel
mailing list