X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=gnulib%2Fimport%2Fgetlogin_r.c;h=a94282ca60f42a8bb516c9543bc305d95017015d;hb=c0c3707ff46ccfb78ea175dd42d628d8c90dca8b;hp=2bafb55fb146c174b1b4d721cb860331feffbf3c;hpb=f81e7e2db6d1aaf47561e54356aee12b585533c2;p=deliverable%2Fbinutils-gdb.git diff --git a/gnulib/import/getlogin_r.c b/gnulib/import/getlogin_r.c index 2bafb55fb1..a94282ca60 100644 --- a/gnulib/import/getlogin_r.c +++ b/gnulib/import/getlogin_r.c @@ -1,6 +1,6 @@ /* Provide a working getlogin_r for systems which lack it. - Copyright (C) 2005-2007, 2010-2016 Free Software Foundation, Inc. + Copyright (C) 2005-2007, 2010-2019 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,7 +13,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, see . */ + along with this program; if not, see . */ /* Written by Paul Eggert, Derek Price, and Bruno Haible. */ @@ -25,7 +25,9 @@ #include #include -#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +#include "malloca.h" + +#if defined _WIN32 && ! defined __CYGWIN__ # define WIN32_LEAN_AND_MEAN # include #else @@ -39,7 +41,7 @@ int getlogin_r (char *name, size_t size) { #undef getlogin_r -#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +#if defined _WIN32 && ! defined __CYGWIN__ /* Native Windows platform. */ DWORD sz; @@ -63,9 +65,27 @@ getlogin_r (char *name, size_t size) /* Platform with a getlogin_r() function. */ int ret = getlogin_r (name, size); - if (ret == 0 && memchr (name, '\0', size) == NULL) - /* name contains a truncated result. */ - return ERANGE; + if (ret == 0) + { + const char *nul = memchr (name, '\0', size); + if (nul == NULL) + /* name contains a truncated result. */ + return ERANGE; + if (size > 0 && nul == name + size - 1) + { + /* strlen(name) == size-1. Determine whether the untruncated result + would have had length size-1 or size. */ + char *room = (char *) malloca (size + 1); + if (room == NULL) + return ENOMEM; + ret = getlogin_r (room, size + 1); + /* The untruncated result should be the same as in the first call. */ + if (ret == 0 && memcmp (name, room, size) != 0) + /* The untruncated result would have been different. */ + ret = ERANGE; + freea (room); + } + } return ret; #else /* Platform with a getlogin() function. */