am 03dd63bd: am bbdfa518: Merge "gethostname.c: change to report ENAMETOOLONG error when buflen is less"
* commit '03dd63bda8846db77cab6b4f17c8a2478f7ac65c': gethostname.c: change to report ENAMETOOLONG error when buflen is less
This commit is contained in:
commit
298dfb6f9d
@ -43,7 +43,6 @@ libc_common_src_files := \
|
|||||||
bionic/ether_aton.c \
|
bionic/ether_aton.c \
|
||||||
bionic/ether_ntoa.c \
|
bionic/ether_ntoa.c \
|
||||||
bionic/fts.c \
|
bionic/fts.c \
|
||||||
bionic/gethostname.c \
|
|
||||||
bionic/getpriority.c \
|
bionic/getpriority.c \
|
||||||
bionic/if_indextoname.c \
|
bionic/if_indextoname.c \
|
||||||
bionic/if_nametoindex.c \
|
bionic/if_nametoindex.c \
|
||||||
@ -120,6 +119,7 @@ libc_bionic_src_files := \
|
|||||||
bionic/getauxval.cpp \
|
bionic/getauxval.cpp \
|
||||||
bionic/getcwd.cpp \
|
bionic/getcwd.cpp \
|
||||||
bionic/getentropy_linux.c \
|
bionic/getentropy_linux.c \
|
||||||
|
bionic/gethostname.cpp \
|
||||||
bionic/getpgrp.cpp \
|
bionic/getpgrp.cpp \
|
||||||
bionic/getpid.cpp \
|
bionic/getpid.cpp \
|
||||||
bionic/gettid.cpp \
|
bionic/gettid.cpp \
|
||||||
|
@ -25,27 +25,24 @@
|
|||||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
int gethostname(char* buff, size_t buflen)
|
int gethostname(char* buf, size_t n) {
|
||||||
{
|
struct utsname name;
|
||||||
struct utsname name;
|
if (uname(&name) == -1) {
|
||||||
int result = 0;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
result = uname(&name);
|
size_t name_length = static_cast<size_t>(strlen(name.nodename) + 1);
|
||||||
if (result != -1)
|
if (name_length > n) {
|
||||||
{
|
errno = ENAMETOOLONG;
|
||||||
int namelen = strlen(name.nodename);
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if ((int)buflen < namelen+1) {
|
memcpy(buf, name.nodename, name_length);
|
||||||
errno = EINVAL;
|
return 0;
|
||||||
result = -1;
|
|
||||||
} else {
|
|
||||||
memcpy( buff, name.nodename, namelen+1 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
@ -482,21 +482,23 @@ TEST(unistd, sethostname) {
|
|||||||
|
|
||||||
TEST(unistd, gethostname) {
|
TEST(unistd, gethostname) {
|
||||||
char hostname[HOST_NAME_MAX + 1];
|
char hostname[HOST_NAME_MAX + 1];
|
||||||
|
|
||||||
memset(hostname, 0, sizeof(hostname));
|
memset(hostname, 0, sizeof(hostname));
|
||||||
|
|
||||||
|
// Can we get the hostname with a big buffer?
|
||||||
ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
|
ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
|
||||||
|
|
||||||
|
// Can we get the hostname with a right-sized buffer?
|
||||||
|
errno = 0;
|
||||||
|
ASSERT_EQ(0, gethostname(hostname, strlen(hostname) + 1));
|
||||||
|
|
||||||
|
// Does uname(2) agree?
|
||||||
utsname buf;
|
utsname buf;
|
||||||
ASSERT_EQ(0, uname(&buf));
|
ASSERT_EQ(0, uname(&buf));
|
||||||
ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
|
ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
|
||||||
ASSERT_GT(strlen(hostname), 0U);
|
ASSERT_GT(strlen(hostname), 0U);
|
||||||
|
|
||||||
|
// Do we correctly detect truncation?
|
||||||
errno = 0;
|
errno = 0;
|
||||||
ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
|
ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
|
||||||
ASSERT_EQ(ENAMETOOLONG, errno);
|
ASSERT_EQ(ENAMETOOLONG, errno);
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
ASSERT_EQ(0, gethostname(hostname, -2));
|
|
||||||
ASSERT_EQ(0, errno);
|
|
||||||
GTEST_LOG_(INFO) << "hostname=" << hostname << "\n";
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user