From e7039330c86c3c24f48d635ca7664b7d45a145c3 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 25 Sep 2003 10:48:39 +0000 Subject: [PATCH] Return -1/EINVAL when the buffer is too small. --- lib/getdomainname.c | 14 +++++++++++++- lib/getdomainname.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/getdomainname.c b/lib/getdomainname.c index 354aa29a42..ce1e200a66 100644 --- a/lib/getdomainname.c +++ b/lib/getdomainname.c @@ -25,6 +25,7 @@ #include "getdomainname.h" #include +#include /* Return the NIS domain name of the machine. WARNING! The NIS domain name is unrelated to the fully qualified host name @@ -33,10 +34,21 @@ Put up to LEN bytes of the NIS domain name into NAME. Null terminate it if the name is shorter than LEN. + If the NIS domain name is longer than LEN, set errno = EINVAL and return -1. Return 0 if successful, otherwise set errno and return -1. */ int getdomainname (char *name, size_t len) { - strncpy (name, "", len); /* Hardcode your domain name if you want. */ + const char *result = ""; /* Hardcode your domain name if you want. */ + size_t result_len = strlen (result); + + if (result_len > len) + { + errno = EINVAL; + return -1; + } + memcpy (name, result, result_len); + if (result_len < len) + name[result_len] = '\0'; return 0; } diff --git a/lib/getdomainname.h b/lib/getdomainname.h index f956e3064d..cf8e33c3f6 100644 --- a/lib/getdomainname.h +++ b/lib/getdomainname.h @@ -34,6 +34,7 @@ Put up to LEN bytes of the NIS domain name into NAME. Null terminate it if the name is shorter than LEN. + If the NIS domain name is longer than LEN, set errno = EINVAL and return -1. Return 0 if successful, otherwise set errno and return -1. */ extern int getdomainname(char *name, size_t len); -- 2.30.2