INN commit: trunk/lib (getnameinfo.c setenv.c)
INN Commit
rra at isc.org
Sat Aug 23 18:35:02 UTC 2014
Date: Saturday, August 23, 2014 @ 11:35:02
Author: iulius
Revision: 9655
sync with latest rra-c-util
Avoid strlcpy in the getnameinfo replacement (use memcpy instead) and
the setenv replacement (use asprintf instead).
Modified:
trunk/lib/getnameinfo.c
trunk/lib/setenv.c
---------------+
getnameinfo.c | 19 +++++++++++++------
setenv.c | 23 ++++++++---------------
2 files changed, 21 insertions(+), 21 deletions(-)
Modified: getnameinfo.c
===================================================================
--- getnameinfo.c 2014-08-23 08:25:48 UTC (rev 9654)
+++ getnameinfo.c 2014-08-23 18:35:02 UTC (rev 9655)
@@ -62,12 +62,15 @@
static bool
try_name(const char *name, char *node, socklen_t nodelen, int *status)
{
+ size_t namelen;
+
if (strchr(name, '.') == NULL)
return false;
- if (strlen(name) + 1 > (size_t) nodelen)
+ namelen = strlen(name);
+ if (namelen + 1 > (size_t) nodelen)
*status = EAI_OVERFLOW;
else {
- strlcpy(node, name, nodelen);
+ memcpy(node, name, namelen + 1);
*status = 0;
}
return true;
@@ -86,6 +89,7 @@
char **alias;
int status;
char *name;
+ size_t namelen;
/* Do the name lookup first unless told not to. */
if (!(flags & NI_NUMERICHOST)) {
@@ -112,9 +116,10 @@
/* Just convert the address to ASCII. */
name = inet_ntoa(*addr);
- if (strlen(name) + 1 > (size_t) nodelen)
+ namelen = strlen(name);
+ if (namelen + 1 > (size_t) nodelen)
return EAI_OVERFLOW;
- strlcpy(node, name, nodelen);
+ memcpy(node, name, namelen + 1);
return 0;
}
@@ -130,15 +135,17 @@
struct servent *srv;
const char *protocol;
int status;
+ size_t namelen;
/* Do the name lookup first unless told not to. */
if (!(flags & NI_NUMERICSERV)) {
protocol = (flags & NI_DGRAM) ? "udp" : "tcp";
srv = getservbyport(htons(port), protocol);
if (srv != NULL) {
- if (strlen(srv->s_name) + 1 > (size_t) servicelen)
+ namelen = strlen(srv->s_name);
+ if (namelen + 1 > (size_t) servicelen)
return EAI_OVERFLOW;
- strlcpy(service, srv->s_name, servicelen);
+ memcpy(service, srv->s_name, namelen + 1);
return 0;
}
}
Modified: setenv.c
===================================================================
--- setenv.c 2014-08-23 08:25:48 UTC (rev 9654)
+++ setenv.c 2014-08-23 18:35:02 UTC (rev 9655)
@@ -35,29 +35,22 @@
setenv(const char *name, const char *value, int overwrite)
{
char *envstring;
- size_t size;
+ /* Do nothing if not overwriting and the variable is already set. */
if (!overwrite && getenv(name) != NULL)
return 0;
/*
- * Allocate memory for the environment string. We intentionally don't use
- * the xmalloc family of allocation routines here, since the intention is
- * to provide a replacement for the standard library function that sets
- * errno and returns in the event of a memory allocation failure.
- */
- size = strlen(name) + 1 + strlen(value) + 1;
- envstring = malloc(size);
- if (envstring == NULL)
- return -1;
-
- /*
* Build the environment string and add it to the environment using
* putenv. Systems without putenv lose, but XPG4 requires it.
+ *
+ * We intentionally don't use the xmalloc family of allocation routines
+ * here, since the intention is to provide a replacement for the standard
+ * library function that sets errno and returns in the event of a memory
+ * allocation failure.
*/
- strlcpy(envstring, name, size);
- strlcat(envstring, "=", size);
- strlcat(envstring, value, size);
+ if (asprintf(&envstring, "%s=%s", name, value) < 0)
+ return -1;
return putenv(envstring);
/*
More information about the inn-committers
mailing list