[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: getaddrinfo.c: don't require snprintf; new function: shorttostr
From: |
Jim Meyering |
Subject: |
Re: getaddrinfo.c: don't require snprintf; new function: shorttostr |
Date: |
Sat, 21 Oct 2006 15:04:11 +0200 |
Simon Josefsson <address@hidden> wrote:
> On Sat, 2006-10-21 at 10:39 +0200, Jim Meyering wrote:
>> Paul, Simon,
>> Any objection to the changes below?
>
> Seems generally fine to me, thanks!
>
>> + short int port = ntohs (((const struct sockaddr_in *) sa)->sin_port);
>
> Shouldn't 'short int' be either 'unsigned short int' or 'uint16_t'?
>
> My ntohs man page says it returns uint16_t, although sin_port is
> 'unsigned short int' on my Linux system (perhaps Linux uses different
> sin_port types depending on platforms, to make sure it is always 16
> bits?).
>
> In any case, it should probably be unsigned.
Good point.
> I'm leaning towards uint16_t, because abstract port numbers is a 16-bit
> integer.
How about the following patch instead?
I've added uinttostr instead of shorttostr, thinking that the
former is more likely to be useful in some other context.
If you prefer uint16_t over "unsigned short int" we can try that, but
since that type is used solely for a temporary, with no risk of sign
extension its width shouldn't matter, and with the latter, we don't
have to worry about whether the type is defined. We could even use
"unsigned int".
BTW, this patch corrects an omission in the previous one,
by using the new, empty gl_PREREQ_* stub.
2006-10-21 Jim Meyering <address@hidden>
* lib/getaddrinfo.c (getnameinfo): Use new lightweight uinttostr,
in place of snprintf.
* modules/inttostr (Files): Add lib/uinttostr.c.
* lib/uinttostr.c (inttostr): New file/function.
* lib/inttostr.h (uinttostr): Declare.
* m4/inttostr.m4: Add AC_LIBOBJ([uinttostr]).
* MODULES.html.sh (Numeric conversion functions <stdlib.h>):
Add uinttostr.
Index: lib/getaddrinfo.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/getaddrinfo.c,v
retrieving revision 1.16
diff -u -r1.16 getaddrinfo.c
--- lib/getaddrinfo.c 18 Sep 2006 18:07:05 -0000 1.16
+++ lib/getaddrinfo.c 21 Oct 2006 12:26:21 -0000
@@ -39,7 +39,7 @@
#define N_(String) String
#include "inet_ntop.h"
-#include "snprintf.h"
+#include "intprops.h"
#include "strdup.h"
/* BeOS has AF_INET, but not PF_INET. */
@@ -405,10 +405,15 @@
#if HAVE_IPV6
case AF_INET6:
#endif
- if (snprintf (service, servicelen, "%d",
- ntohs (((const struct sockaddr_in *) sa)->sin_port))
- + 1 > servicelen)
- return EAI_OVERFLOW;
+ {
+ unsigned short int port
+ = ntohs (((const struct sockaddr_in *) sa)->sin_port);
+ char buf[INT_BUFSIZE_BOUND (port)];
+ char const *s = uinttostr (port, buf);
+ if (strlen (s) + 1 > servicelen)
+ return EAI_OVERFLOW;
+ memcpy (service, s, strlen (s) + 1);
+ }
break;
}
Index: lib/uinttostr.c
===================================================================
RCS file: lib/uinttostr.c
diff -N lib/uinttostr.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/uinttostr.c 21 Oct 2006 12:26:21 -0000
@@ -0,0 +1,3 @@
+#define inttostr uinttostr
+#define inttype unsigned int
+#include "inttostr.c"
Index: lib/inttostr.h
===================================================================
RCS file: /sources/gnulib/gnulib/lib/inttostr.h,v
retrieving revision 1.7
diff -u -r1.7 inttostr.h
--- lib/inttostr.h 21 Aug 2006 06:11:26 -0000 1.7
+++ lib/inttostr.h 21 Oct 2006 12:26:21 -0000
@@ -27,3 +27,4 @@
char *offtostr (off_t, char *);
char *imaxtostr (intmax_t, char *);
char *umaxtostr (uintmax_t, char *);
+char *uinttostr (unsigned int, char *);
Index: modules/inttostr
===================================================================
RCS file: /sources/gnulib/gnulib/modules/inttostr,v
retrieving revision 1.11
diff -u -r1.11 inttostr
--- modules/inttostr 13 Oct 2006 12:40:23 -0000 1.11
+++ modules/inttostr 21 Oct 2006 12:26:21 -0000
@@ -7,6 +7,7 @@
lib/inttostr.h
lib/offtostr.c
lib/umaxtostr.c
+lib/uinttostr.c
m4/inttostr.m4
Depends-on:
Index: m4/inttostr.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/inttostr.m4,v
retrieving revision 1.5
diff -u -r1.5 inttostr.m4
--- m4/inttostr.m4 21 Aug 2006 06:11:26 -0000 1.5
+++ m4/inttostr.m4 21 Oct 2006 12:26:21 -0000
@@ -1,4 +1,4 @@
-#serial 6
+#serial 7
dnl Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -9,11 +9,13 @@
AC_LIBOBJ([imaxtostr])
AC_LIBOBJ([offtostr])
AC_LIBOBJ([umaxtostr])
+ AC_LIBOBJ([uinttostr])
gl_PREREQ_INTTOSTR
gl_PREREQ_IMAXTOSTR
gl_PREREQ_OFFTOSTR
gl_PREREQ_UMAXTOSTR
+ gl_PREREQ_UINTTOSTR
])
# Prerequisites of lib/inttostr.h.
@@ -30,3 +32,6 @@
# Prerequisites of lib/umaxtostr.c.
AC_DEFUN([gl_PREREQ_UMAXTOSTR], [:])
+
+# Prerequisites of lib/uinttostr.c.
+AC_DEFUN([gl_PREREQ_UINTTOSTR], [:])
Index: MODULES.html.sh
===================================================================
RCS file: /sources/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.152
diff -u -r1.152 MODULES.html.sh
--- MODULES.html.sh 16 Oct 2006 11:55:35 -0000 1.152
+++ MODULES.html.sh 21 Oct 2006 12:26:21 -0000
@@ -1802,6 +1802,7 @@
func_begin_table
func_module intprops
func_module inttostr
+ func_module uinttostr
func_module xstrtoimax
func_module xstrtoumax
func_end_table