[PATCH] malloced getpw/grxxx functions for bb

tito farmatito at tiscali.it
Mon Dec 29 21:19:51 UTC 2014


On Saturday 06 December 2014 15:24:01 tito wrote:
> On Sunday 09 November 2014 20:56:07 tito wrote:
> > On Sunday 28 September 2014 15:24:50 tito wrote:
> > > On Saturday 27 September 2014 14:58:08 tito wrote:
> > > > On Wednesday 24 September 2014 23:02:55 tito wrote:
> > > > > On Saturday 20 September 2014 16:32:01 tito wrote:
> > > > > > Hi,
> > > > > > One more fix of a return value.
> > > > > 
> > > > > Hi,
> > > > > more return value and errno fixes.
> > > > > 
> > > > Hi,
> > > > make the tokenize function more robust.
> > > > 
> > > Hi,
> > > more minor errno fixes and code cleanups.
> > > 
> > Hi,
> > is there anybody bold out there that wants to review
> > this stuff?
> > 
> Hi,
> make the  parse_common function more robust.
> 
Hi,
after putting more work at this malloced libpwdgrp functions I think they  now are
pretty robust, with no memory leaks and able to handle more or less gracefully
problematic passwd/group/shadow files. The main features of this
implemantation are:

* 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically
 *    allocated and reused by later calls. if ERANGE error pops up it is
 *    reallocated to the size of the longest line found so far in the
 *    passwd/group files and reused for later calls.
 *    If ENABLE_FEATURE_CLEAN_UP is set the buffers are freed at program
 *    exit using the atexit function to make valgrind happy.
  * 2) the passwd/group files:
 *      a) must contain the expected number of fields (as per count of field
 *         delimeters ":") or we will complain with a error message.
 *      b) leading or trailing whitespace in fields is allowed and handled.
 *      c) some fields are not allowed to be empty (e.g. username, uid/gid,
 *         homedir, shell) and in this case NULL is returned and errno is
 *         set to EINVAL. This behaviour could be easily changed by
 *         modifying PW_DEF, GR_DEF, SP_DEF strings (uppercase
 *         makes a field mandatory).
 *      d) the string representing uid/gid must be convertible by strtoXX
 *         functions or NULL is returned and errno is set to EINVAL.
 *      e) leading or trailing whitespaces in member names and empty members
 *         are allowed and handled.
 * 3) the internal function for getgrouplist uses a dynamically allocated
 *    buffer and retries with a bigger one in case it is to small;
 * 4) the _r functions use the user supplied buffers that are never reallocated
 *    but use mostly the same common code as the other functions.
 * 5) at the moment only the functions really used by busybox code are
 *    implemented, if you need a particular missing function it should be
 *    easy to write it by using the internal common code.

I've done some testing and everything seems to work as expected
nonetheless some more testing by the list members and a good
code review by more experienced programmers is needed as this
lib calls stuff is very new for me and maybe I overlooked 
something obvious.
Attached you will find a patch and a drop in replacement
for pwd_grp.c for easier review and testing.

Size increase is about 120 bytes due to the increased feature set,
but the bloat-o-meter output looks somewhat suspicious:

./scripts/bloat-o-meter busybox_unstripped_original busybox_unstripped
function                                             old     new   delta
convert_to_struct                                      -     351    +351
parse_common                                           -     214    +214
tokenize                                               -     136    +136
getgrouplist_internal                                185     309    +124
getpw_common                                           -      96     +96
getgr_common                                           -      96     +96
getpw_common_malloc                                    -      78     +78
getgr_common_malloc                                    -      78     +78
parse_file                                             -      75     +75
.rodata                                           142136  142185     +49
bb_internal_getpwent_r                               102     147     +45
my_pwd                                                 -      28     +28
my_grp                                                 -      16     +16
pwd_buffer_size                                        -       4      +4
pwd_buffer                                             -       4      +4
my_pw                                                  -       4      +4
my_gr                                                  -       4      +4
grp_buffer_size                                        -       4      +4
grp_buffer                                             -       4      +4
gr_off                                                 3       4      +1
sp_off                                                 9       8      -1
ptr_to_statics                                         4       -      -4
bb_internal_getpwuid                                  37      19     -18
bb_internal_getspnam_r                               119      97     -22
bb_internal_getgrgid                                  43      19     -24
bb_internal_getpwnam                                  37      11     -26
get_S                                                 30       -     -30
bb_internal_getgrnam                                  43      11     -32
bb_internal_getpwuid_r                               111       -    -111
bb_internal_getgrgid_r                               111       -    -111
bb__parsepwent                                       112       -    -112
bb_internal_getpwnam_r                               119       -    -119
bb_internal_getgrnam_r                               119       -    -119
bb__parsespent                                       124       -    -124
bb__pgsreader                                        194       -    -194
bb__parsegrent                                       246       -    -246
------------------------------------------------------------------------------
(add/remove: 16/10 grow/shrink: 4/6 up/down: 1411/-1293)      Total: 118 bytes


Critics, hints, improvements are welcome.

Ciao,
Tito

-------------- next part --------------
A non-text attachment was scrubbed...
Name: pwd_grp.patch
Type: text/x-patch
Size: 39235 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20141229/d5fc6a96/attachment-0001.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pwd_grp.c
Type: text/x-csrc
Size: 16243 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20141229/d5fc6a96/attachment-0001.c>


More information about the busybox mailing list