Skip to content

Commit db2f315

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 282bf16 commit db2f315

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
@@ -32,33 +32,28 @@ struct group *
3232
sgetgrent(const char *s)
3333
{
3434
static char *buf = NULL;
35-
static struct group *grent = NULL;
35+
static struct group grent = {};
3636

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

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

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

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

61-
return grent;
56+
return &grent;
6257
}
6358

6459

lib/shadow/gshadow/sgetsgent.c

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

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

4242
n = strchrcnt(s, ',') + 4;
43-
lssize = n * sizeof(char *); // For 'sgent->sg_adm' and 'sgent->sg_mem'
43+
lssize = n * sizeof(char *); // For 'sgent.sg_adm' and 'sgent.sg_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(sgent);
52-
sgent = MALLOC(1, struct sgrp);
53-
if (sgent == NULL)
54-
return NULL;
55-
56-
e = sgetsgent_r(s, sgent, buf, size);
51+
e = sgetsgent_r(s, &sgent, buf, size);
5752
if (e != 0) {
5853
errno = e;
5954
return NULL;
6055
}
6156

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

6560

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)