Skip to content

Commit

Permalink
grp: Rewrite to use struct scratch_buffer instead of extend_alloca
Browse files Browse the repository at this point in the history
grp/compat-initgroups.c is included from nscd/initgrcache.c, which is
why the #include directive has to be added there as well.
  • Loading branch information
Florian Weimer committed Apr 8, 2015
1 parent 7b8399f commit 866ba63
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
* elf/pldd.c (main): Rewrite to use struct
scratch_buffer instead of extend_alloca.
* elf/pldd-xx.c (find_maps): Likewise.
* grp/initgroups.c: Include <scratch_buffer.h> instead of
<alloca.h>.
* grp/compat-initgroups.c (compat_call): Rewrite to use struct
scratch_buffer instead of extend_alloca.
* nscd/initgrcache.c: Include <scratch_buffer.h>, now needed by
grp/compat-initgroups.c.

2015-04-08 Joseph Myers <joseph@codesourcery.com>

Expand Down
26 changes: 8 additions & 18 deletions grp/compat-initgroups.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ compat_call (service_user *nip, const char *user, gid_t group, long int *start,
long int *size, gid_t **groupsp, long int limit, int *errnop)
{
struct group grpbuf;
size_t buflen = __sysconf (_SC_GETGR_R_SIZE_MAX);
enum nss_status status;
set_function setgrent_fct;
get_function getgrent_fct;
Expand All @@ -35,30 +34,22 @@ compat_call (service_user *nip, const char *user, gid_t group, long int *start,

endgrent_fct = __nss_lookup_function (nip, "endgrent");

char *tmpbuf = __alloca (buflen);
bool use_malloc = false;
struct scratch_buffer tmpbuf;
scratch_buffer_init (&tmpbuf);
enum nss_status result = NSS_STATUS_SUCCESS;

do
{
while ((status = DL_CALL_FCT (getgrent_fct,
(&grpbuf, tmpbuf, buflen, errnop)),
(&grpbuf, tmpbuf.data, tmpbuf.length,
errnop)),
status == NSS_STATUS_TRYAGAIN)
&& *errnop == ERANGE)
{
if (__libc_use_alloca (buflen * 2))
tmpbuf = extend_alloca (tmpbuf, buflen, buflen * 2);
else
if (!scratch_buffer_grow (&tmpbuf))
{
buflen *= 2;
char *newbuf = realloc (use_malloc ? tmpbuf : NULL, buflen);
if (newbuf == NULL)
{
result = NSS_STATUS_TRYAGAIN;
goto done;
}
use_malloc = true;
tmpbuf = newbuf;
result = NSS_STATUS_TRYAGAIN;
goto done;
}
}

Expand Down Expand Up @@ -116,8 +107,7 @@ compat_call (service_user *nip, const char *user, gid_t group, long int *start,
while (status == NSS_STATUS_SUCCESS);

done:
if (use_malloc)
free (tmpbuf);
scratch_buffer_free (&tmpbuf);

if (endgrent_fct)
DL_CALL_FCT (endgrent_fct, ());
Expand Down
2 changes: 1 addition & 1 deletion grp/initgroups.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */

#include <alloca.h>
#include <assert.h>
#include <errno.h>
#include <grp.h>
Expand All @@ -26,6 +25,7 @@
#include <sys/param.h>
#include <sys/types.h>
#include <nsswitch.h>
#include <scratch_buffer.h>

#include "../nscd/nscd-client.h"
#include "../nscd/nscd_proto.h"
Expand Down
1 change: 1 addition & 0 deletions nscd/initgrcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <scratch_buffer.h>

#include "dbg_log.h"
#include "nscd.h"
Expand Down

0 comments on commit 866ba63

Please sign in to comment.