Skip to content

Commit c6713c5

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 6006b0c commit c6713c5

File tree

4 files changed

+23
-43
lines changed

4 files changed

+23
-43
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: 7 additions & 12 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

@@ -90,8 +85,8 @@ sgetsgent_r(size_t size;
9085
if (STRSEP2ARR(p, ":", fields) == -1)
9186
return EINVAL;
9287

93-
sgent->sg_namp = fields[0];
94-
sgent->sg_passwd = fields[1];
88+
sgroup.sg_namp = fields[0];
89+
sgroup.sg_passwd = fields[1];
9590

9691
if (!is_aligned(buf, char *))
9792
return EINVAL;
@@ -102,7 +97,7 @@ sgetsgent_r(size_t size;
10297
if (csv2ls(fields[2], nadm, sgent->sg_adm) == -1)
10398
return errno;
10499

105-
sgent->sg_mem = sgent->sg_adm + nadm;
100+
sgroup.sg_mem = sgroup.sg_adm + nadm;
106101
nmem = strchrcnt(fields[3], ',') + 2;
107102
if (nmem + nadm > n)
108103
return E2BIG;

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
size_t size;
3939

@@ -44,18 +44,13 @@ sgetpwent(const char *s)
4444
if (buf == NULL)
4545
return NULL;
4646

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

58-
return pwent;
53+
return &pwent;
5954
}
6055

6156

lib/shadow/shadow/sgetspent.c

Lines changed: 9 additions & 14 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 spwd;
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

@@ -91,8 +86,8 @@ sgetspent_r(size_t size;
9186
if (i != countof(fields) && i != OFIELDS)
9287
return EINVAL;
9388

94-
spent->sp_namp = fields[0];
95-
spent->sp_pwdp = fields[1];
89+
spwd.sp_namp = fields[0];
90+
spwd.sp_pwdp = fields[1];
9691

9792
if (streq(fields[2], ""))
9893
spent->sp_lstchg = -1;
@@ -115,10 +110,10 @@ sgetspent_r(size_t size;
115110
* /etc/shadow formatted file. Initialize the other
116111
* field members to -1.
117112
*/
118-
spent->sp_warn = -1;
119-
spent->sp_inact = -1;
120-
spent->sp_expire = -1;
121-
spent->sp_flag = SHADOW_SP_FLAG_UNSET;
113+
spwd.sp_warn = -1;
114+
spwd.sp_inact = -1;
115+
spwd.sp_expire = -1;
116+
spwd.sp_flag = SHADOW_SP_FLAG_UNSET;
122117

123118
return 0;
124119
}

0 commit comments

Comments
 (0)