Skip to content

Commit 5124f13

Browse files
Revert "lib/shadow/: Allocate the structure"
This reverts HEAD^^ (2025-09-29; "lib/shadow/: Allocate the structure"). That change was temporary to allow adding a re-entrant variant of the function with a small diff. Remove it now that it's done. We don't want to be allocating more than necessary. Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 353c600 commit 5124f13

File tree

4 files changed

+14
-34
lines changed

4 files changed

+14
-34
lines changed

lib/shadow/group/sgetgrent.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,28 @@ struct group *
3333
sgetgrent(const char *s)
3434
{
3535
static char *buf = NULL;
36-
static struct group *grent = NULL;
36+
static struct group grent = {};
3737

3838
int e;
3939
char *p, *end;
4040
size_t n, lssize, size;
4141

4242
n = strchrcnt(s, ',') + 2;
43-
lssize = n * sizeof(char *); // For 'grent->gr_mem'.
43+
lssize = n * sizeof(char *); // For 'grent.gr_mem'.
4444
size = lssize + strlen(s) + 1;
4545

4646
free(buf);
4747
buf = MALLOC(size, char);
4848
if (buf == NULL)
4949
return NULL;
5050

51-
free(grent);
52-
grent = MALLOC(1, struct group);
53-
if (grent == NULL)
54-
return NULL;
55-
56-
e = sgetgrent_r(s, grent, buf, size);
51+
e = sgetgrent_r(s, &grent, buf, size);
5752
if (e != 0) {
5853
errno = e;
5954
return NULL;
6055
}
6156

62-
return grent;
57+
return &grent;
6358
}
6459

6560

lib/shadow/gshadow/sgetsgent.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,28 @@ struct sgrp *
3434
sgetsgent(const char *s)
3535
{
3636
static char *buf = NULL;
37-
static struct sgrp *sgent = NULL;
37+
static struct sgrp sgent = {};
3838

3939
int e;
4040
char *p, *end;
4141
size_t n, nadm, nmem, lssize, size;
4242

4343
n = strchrcnt(s, ',') + 4;
44-
lssize = n * sizeof(char *); // For 'sgent->sg_adm' and 'sgent->sg_mem'
44+
lssize = n * sizeof(char *); // For 'sgent.sg_adm' and 'sgent.sg_mem'
4545
size = lssize + strlen(s) + 1;
4646

4747
free(buf);
4848
buf = MALLOC(size, char);
4949
if (buf == NULL)
5050
return NULL;
5151

52-
free(sgent);
53-
sgent = MALLOC(1, struct sgrp);
54-
if (sgent == NULL)
55-
return NULL;
56-
57-
e = sgetsgent_r(s, sgent, buf, size);
52+
e = sgetsgent_r(s, &sgent, buf, size);
5853
if (e != 0) {
5954
errno = e;
6055
return NULL;
6156
}
6257

63-
return sgent;
58+
return &sgent;
6459
}
6560

6661

lib/shadow/passwd/sgetpwent.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct passwd *
3333
sgetpwent(const char *s)
3434
{
3535
static char *buf = NULL;
36-
static struct passwd *pwent = NULL;
36+
static struct passwd pwent = {};
3737

3838
int e;
3939
size_t size;
@@ -45,18 +45,13 @@ sgetpwent(const char *s)
4545
if (buf == NULL)
4646
return NULL;
4747

48-
free(pwent);
49-
pwent = MALLOC(1, struct passwd);
50-
if (pwent == NULL)
51-
return NULL;
52-
53-
e = sgetpwent_r(s, pwent, buf, size);
48+
e = sgetpwent_r(s, &pwent, buf, size);
5449
if (e != 0) {
5550
errno = e;
5651
return NULL;
5752
}
5853

59-
return pwent;
54+
return &pwent;
6055
}
6156

6257

lib/shadow/shadow/sgetspent.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct spwd *
3838
sgetspent(const char *s)
3939
{
4040
static char *buf = NULL;
41-
static struct spwd *spent = NULL;
41+
static struct spwd spent = {};
4242

4343
int e;
4444
size_t size;
@@ -50,18 +50,13 @@ sgetspent(const char *s)
5050
if (buf == NULL)
5151
return NULL;
5252

53-
free(spent);
54-
spent = MALLOC(1, struct spwd);
55-
if (spent == NULL)
56-
return NULL;
57-
58-
e = sgetspent_r(s, spent, buf, size, dummy);
53+
e = sgetspent_r(s, &spent, buf, size, dummy);
5954
if (e != 0) {
6055
errno = e;
6156
return NULL;
6257
}
6358

64-
return spent;
59+
return &spent;
6560
}
6661
#endif
6762

0 commit comments

Comments
 (0)