nss_files: Use struct scratch_buffer instead of extend_alloca [BZ #18023]

This commit is contained in:
Florian Weimer 2018-06-25 19:29:11 +02:00
parent 1599ed4e95
commit 43b1048ab9
2 changed files with 16 additions and 22 deletions

View File

@ -1,3 +1,9 @@
2018-06-25 Florian Weimer <fweimer@redhat.com>
[BZ #18023]
* nss/nss_files/files-initgroups.c (_nss_files_initgroups_dyn):
Use struct scratch_buffer instead of extend_alloca.
2018-06-25 Florian Weimer <fweimer@redhat.com> 2018-06-25 Florian Weimer <fweimer@redhat.com>
[BZ #18023] [BZ #18023]

View File

@ -16,7 +16,6 @@
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */ <http://www.gnu.org/licenses/>. */
#include <alloca.h>
#include <errno.h> #include <errno.h>
#include <grp.h> #include <grp.h>
#include <nss.h> #include <nss.h>
@ -25,6 +24,7 @@
#include <sys/param.h> #include <sys/param.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <scratch_buffer.h>
enum nss_status enum nss_status
_nss_files_initgroups_dyn (const char *user, gid_t group, long int *start, _nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
@ -46,9 +46,8 @@ _nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
enum nss_status status = NSS_STATUS_SUCCESS; enum nss_status status = NSS_STATUS_SUCCESS;
bool any = false; bool any = false;
size_t buflen = 1024; struct scratch_buffer tmpbuf;
void *buffer = alloca (buflen); scratch_buffer_init (&tmpbuf);
bool buffer_use_malloc = false;
gid_t *groups = *groupsp; gid_t *groups = *groupsp;
@ -67,26 +66,16 @@ _nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
} }
struct group grp; struct group grp;
int res = _nss_files_parse_grent (line, &grp, buffer, buflen, errnop); int res = _nss_files_parse_grent (line, &grp,
tmpbuf.data, tmpbuf.length, errnop);
if (res == -1) if (res == -1)
{ {
size_t newbuflen = 2 * buflen; if (!scratch_buffer_grow (&tmpbuf))
if (buffer_use_malloc || ! __libc_use_alloca (buflen + newbuflen))
{ {
void *newbuf = realloc (buffer_use_malloc ? buffer : NULL, *errnop = ENOMEM;
newbuflen); status = NSS_STATUS_TRYAGAIN;
if (newbuf == NULL) goto out;
{
*errnop = ENOMEM;
status = NSS_STATUS_TRYAGAIN;
goto out;
}
buffer = newbuf;
buflen = newbuflen;
buffer_use_malloc = true;
} }
else
buffer = extend_alloca (buffer, buflen, newbuflen);
/* Reread current line, the parser has clobbered it. */ /* Reread current line, the parser has clobbered it. */
fsetpos (stream, &pos); fsetpos (stream, &pos);
continue; continue;
@ -132,8 +121,7 @@ _nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
out: out:
/* Free memory. */ /* Free memory. */
if (buffer_use_malloc) scratch_buffer_free (&tmpbuf);
free (buffer);
free (line); free (line);
fclose (stream); fclose (stream);