trawick 01/11/20 11:00:40
Modified: . CHANGES
include apr_network_io.h
network_io/os2 sockopt.c
network_io/unix sockopt.c
network_io/win32 sockopt.c
Log:
Tweak apr_gethostname() so that it detects truncation of the
name and returns an error. Such behavior varied between platforms
before, since some systems returned an error if the buffer
wasn't large enough while others just truncated the returned
name and left it up to the app to detect truncation and/or
'\0'-terminate it.
Revision Changes Path
1.185 +3 -0 apr/CHANGES
Index: CHANGES
===================================================================
RCS file: /home/cvs/apr/CHANGES,v
retrieving revision 1.184
retrieving revision 1.185
diff -u -r1.184 -r1.185
--- CHANGES 2001/11/20 08:18:37 1.184
+++ CHANGES 2001/11/20 19:00:39 1.185
@@ -1,5 +1,8 @@
Changes with APR b1
+ *) Tweak apr_gethostname() so that it detects truncation of the
+ name and returns an error. [Jeff Trawick]
+
*) Fix bug in Darwin DSO code. [Sander Temme <[EMAIL PROTECTED]>]
*) Fix apr_setup_signal_thread() to grab the right error code from
1.113 +2 -1 apr/include/apr_network_io.h
Index: apr_network_io.h
===================================================================
RCS file: /home/cvs/apr/include/apr_network_io.h,v
retrieving revision 1.112
retrieving revision 1.113
diff -u -r1.112 -r1.113
--- apr_network_io.h 2001/11/13 19:35:15 1.112
+++ apr_network_io.h 2001/11/20 19:00:39 1.113
@@ -407,8 +407,9 @@
* Get name of the current machine
* @param buf A buffer to store the hostname in.
* @param len The maximum length of the hostname that can be stored in the
- * buffer provided.
+ * buffer provided. The suggested length is APRMAXHOSTLEN + 1.
* @param cont The pool to use.
+ * @remark If the buffer was not large enough, an error will be returned.
*/
APR_DECLARE(apr_status_t) apr_gethostname(char *buf, int len, apr_pool_t
*cont);
1.23 +8 -3 apr/network_io/os2/sockopt.c
Index: sockopt.c
===================================================================
RCS file: /home/cvs/apr/network_io/os2/sockopt.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- sockopt.c 2001/08/10 21:04:48 1.22
+++ sockopt.c 2001/11/20 19:00:39 1.23
@@ -140,9 +140,14 @@
APR_DECLARE(apr_status_t) apr_gethostname(char *buf, apr_int32_t len,
apr_pool_t *cont)
{
- if (gethostname(buf, len) == -1)
+ if (gethostname(buf, len) == -1) {
+ buf[0] = '\0';
return APR_OS2_STATUS(sock_errno());
- else
- return APR_SUCCESS;
+ }
+ else if (!memchr(buf, '\0', len)) { /* buffer too small */
+ buf[0] = '\0';
+ return APR_ENAMETOOLONG;
+ }
+ return APR_SUCCESS;
}
1.49 +11 -3 apr/network_io/unix/sockopt.c
Index: sockopt.c
===================================================================
RCS file: /home/cvs/apr/network_io/unix/sockopt.c,v
retrieving revision 1.48
retrieving revision 1.49
diff -u -r1.48 -r1.49
--- sockopt.c 2001/07/30 17:56:16 1.48
+++ sockopt.c 2001/11/20 19:00:39 1.49
@@ -293,10 +293,18 @@
apr_status_t apr_gethostname(char *buf, apr_int32_t len, apr_pool_t *cont)
{
- if (gethostname(buf, len) == -1)
+ if (gethostname(buf, len) == -1) {
+ buf[0] = '\0';
return errno;
- else
- return APR_SUCCESS;
+ }
+ else if (!memchr(buf, '\0', len)) { /* buffer too small */
+ /* note... most platforms just truncate in this condition
+ * linux+glibc return an error
+ */
+ buf[0] = '\0';
+ return APR_ENAMETOOLONG;
+ }
+ return APR_SUCCESS;
}
#if APR_HAS_SO_ACCEPTFILTER
1.36 +8 -3 apr/network_io/win32/sockopt.c
Index: sockopt.c
===================================================================
RCS file: /home/cvs/apr/network_io/win32/sockopt.c,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -r1.35 -r1.36
--- sockopt.c 2001/08/09 23:26:18 1.35
+++ sockopt.c 2001/11/20 19:00:40 1.36
@@ -234,10 +234,15 @@
APR_DECLARE(apr_status_t) apr_gethostname(char *buf, int len,
apr_pool_t *cont)
{
- if (gethostname(buf, len) == -1)
+ if (gethostname(buf, len) == -1) {
+ buf[0] = '\0';
return apr_get_netos_error();
- else
- return APR_SUCCESS;
+ }
+ else if (!memchr(buf, '\0', len)) { /* buffer too small */
+ buf[0] = '\0';
+ return APR_ENAMETOOLONG;
+ }
+ return APR_SUCCESS;
}