1010
1111#include "shadow/gshadow/sgetsgent.h"
1212
13+ #include <errno.h>
1314#include <stddef.h>
1415#include <string.h>
1516
@@ -34,7 +35,7 @@ sgetsgent(const char *s)
3435 static char * buf = NULL ;
3536 static struct sgrp * sgent = NULL ;
3637
37- char * fields [ 4 ] ;
38+ int e ;
3839 char * p , * end ;
3940 size_t n , nadm , nmem , lssize , size ;
4041
@@ -52,33 +53,62 @@ sgetsgent(const char *s)
5253 if (sgent == NULL )
5354 return NULL ;
5455
56+ e = sgetsgent_r (s , sgent , buf , size );
57+ if (e != 0 ) {
58+ errno = e ;
59+ return NULL ;
60+ }
61+
62+ return sgent ;
63+ }
64+
65+
66+ // from-string get shadow group entry re-entrant
67+ int
68+ sgetsgent_r (size_t size ;
69+ const char * s , struct sgrp * sgent , char buf [size ], size_t size ,
70+ struct sgrp * * dummy )
71+ {
72+ char * fields [4 ];
73+ char * p , * end ;
74+ size_t n , nadm , nmem , lssize , size ;
75+
76+ // The first 'lssize' bytes of 'buf' are used for 'sg_adm' and 'sg_mem'.
77+ n = strchrcnt (s , ',' ) + 4 ;
78+ lssize = n * sizeof (char * );
79+ if (lssize >= size )
80+ return E2BIG ;
81+
82+ // The remaining bytes of 'buf' are used for a copy of 's'.
5583 end = buf + size ;
5684 p = buf + lssize ;
5785 if (stpecpy (p , end , s ) == NULL )
58- return NULL ;
86+ return errno ;
5987
6088 stpsep (p , "\n" );
6189
6290 if (STRSEP2ARR (p , ":" , fields ) == -1 )
63- return NULL ;
91+ return EINVAL ;
6492
6593 sgent -> sg_namp = fields [0 ];
6694 sgent -> sg_passwd = fields [1 ];
6795
96+ if (!is_aligned (buf , char * ))
97+ return EINVAL ;
6898 sgent -> sg_adm = (char * * ) buf ;
6999 nadm = strchrcnt (fields [2 ], ',' ) + 2 ;
70100 if (nadm > n )
71- return NULL ;
101+ return E2BIG ;
72102 if (csv2ls (fields [2 ], nadm , sgent -> sg_adm ) == -1 )
73- return NULL ;
103+ return errno ;
74104
75105 sgent -> sg_mem = sgent -> sg_adm + nadm ;
76106 nmem = strchrcnt (fields [3 ], ',' ) + 2 ;
77107 if (nmem + nadm > n )
78- return NULL ;
108+ return E2BIG ;
79109 if (csv2ls (fields [3 ], nmem , sgent -> sg_mem ) == -1 )
80- return NULL ;
110+ return errno ;
81111
82- return sgent ;
112+ return 0 ;
83113}
84114#endif
0 commit comments