Date: Thu, 8 Jan 2009 02:41:40 +0000 (UTC) From: Sean Farley <scf@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-7@freebsd.org Subject: svn commit: r186883 - stable/7/lib/libutil Message-ID: <200901080241.n082feiC006419@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: scf Date: Thu Jan 8 02:41:40 2009 New Revision: 186883 URL: http://svn.freebsd.org/changeset/base/186883 Log: MFC r184831,185237: style(9) fixes. Fixed style issues with variable ordering and naming, spacing and parentheses. Fixed alignment issue in gr_dup() in its assignment of gr_mem using a struct to force alignment without performing alignment mathematics. This was noticed recently with libutil was built with WARNS=6 on platform such as sparc64. Added checks to gr_dup(), gr_equal() and gr_make() to prevent segfaults when examining struct group's with the struct members pointing to NULL's. Unlike r185237, did not enable WARNS?=6 since other changes would need to be MFC'd first. Reviewed by: des Modified: stable/7/lib/libutil/ (props changed) stable/7/lib/libutil/gr_util.c Modified: stable/7/lib/libutil/gr_util.c ============================================================================== --- stable/7/lib/libutil/gr_util.c Thu Jan 8 00:45:47 2009 (r186882) +++ stable/7/lib/libutil/gr_util.c Thu Jan 8 02:41:40 2009 (r186883) @@ -28,18 +28,21 @@ __FBSDID("$FreeBSD$"); #include <sys/param.h> + #include <grp.h> #include <inttypes.h> +#include <libutil.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <libutil.h> - - -static const char GroupLineFormat[] = "%s:%s:%ju:"; +struct group_storage { + struct group gr; + char *members[]; +}; +static const char group_line_format[] = "%s:%s:%ju:"; /* * Compares two struct group's. @@ -47,41 +50,50 @@ static const char GroupLineFormat[] = "% int gr_equal(const struct group *gr1, const struct group *gr2) { + int gr1_ndx; + int gr2_ndx; bool found; - bool equal; - int gr1Ndx; - int gr2Ndx; /* Check that the non-member information is the same. */ - equal = strcmp(gr1->gr_name, gr2->gr_name) == 0 && - strcmp(gr1->gr_passwd, gr2->gr_passwd) == 0 && - gr1->gr_gid == gr2->gr_gid; + if (gr1->gr_name == NULL || gr2->gr_name == NULL) { + if (gr1->gr_name != gr2->gr_name) + return (false); + } else if (strcmp(gr1->gr_name, gr2->gr_name) != 0) + return (false); + if (gr1->gr_passwd == NULL || gr2->gr_passwd == NULL) { + if (gr1->gr_passwd != gr2->gr_passwd) + return (false); + } else if (strcmp(gr1->gr_passwd, gr2->gr_passwd) != 0) + return (false); + if (gr1->gr_gid != gr2->gr_gid) + return (false); /* Check all members in both groups. */ - if (equal) { - for (found = false, gr1Ndx = 0; gr1->gr_mem[gr1Ndx] != NULL; - gr1Ndx++) { - for (gr2Ndx = 0; gr2->gr_mem[gr2Ndx] != NULL; gr2Ndx++) - if (strcmp(gr1->gr_mem[gr1Ndx], - gr2->gr_mem[gr2Ndx]) == 0) { + if (gr1->gr_mem == NULL || gr2->gr_mem == NULL) { + if (gr1->gr_mem != gr2->gr_mem) + return (false); + } else { + for (found = false, gr1_ndx = 0; gr1->gr_mem[gr1_ndx] != NULL; + gr1_ndx++) { + for (gr2_ndx = 0; gr2->gr_mem[gr2_ndx] != NULL; + gr2_ndx++) + if (strcmp(gr1->gr_mem[gr1_ndx], + gr2->gr_mem[gr2_ndx]) == 0) { found = true; break; } - if (! found) { - equal = false; - break; - } + if (!found) + return (false); } /* Check that group2 does not have more members than group1. */ - if (gr2->gr_mem[gr1Ndx] != NULL) - equal = false; + if (gr2->gr_mem[gr1_ndx] != NULL) + return (false); } - return (equal); + return (true); } - /* * Make a group line out of a struct group. */ @@ -89,82 +101,84 @@ char * gr_make(const struct group *gr) { char *line; + size_t line_size; int ndx; - size_t lineSize; /* Calculate the length of the group line. */ - lineSize = snprintf(NULL, 0, GroupLineFormat, gr->gr_name, + line_size = snprintf(NULL, 0, group_line_format, gr->gr_name, gr->gr_passwd, (uintmax_t)gr->gr_gid) + 1; - for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) - lineSize += strlen(gr->gr_mem[ndx]) + 1; - if (ndx > 0) - lineSize--; + if (gr->gr_mem != NULL) { + for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) + line_size += strlen(gr->gr_mem[ndx]) + 1; + if (ndx > 0) + line_size--; + } /* Create the group line and fill it. */ - if ((line = malloc(lineSize)) == NULL) + if ((line = malloc(line_size)) == NULL) return (NULL); - lineSize = snprintf(line, lineSize, GroupLineFormat, gr->gr_name, + line_size = snprintf(line, line_size, group_line_format, gr->gr_name, gr->gr_passwd, (uintmax_t)gr->gr_gid); - for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { - strcat(line, gr->gr_mem[ndx]); - if (gr->gr_mem[ndx + 1] != NULL) - strcat(line, ","); - } + if (gr->gr_mem != NULL) + for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { + strcat(line, gr->gr_mem[ndx]); + if (gr->gr_mem[ndx + 1] != NULL) + strcat(line, ","); + } return (line); } - /* * Duplicate a struct group. */ struct group * gr_dup(const struct group *gr) { - int ndx; - int numMem; + char *dst; size_t len; - struct group *ngr; + struct group_storage *gs; + int ndx; + int num_mem; - /* Calculate size of group. */ - len = sizeof(*gr) + - (gr->gr_name != NULL ? strlen(gr->gr_name) + 1 : 0) + - (gr->gr_passwd != NULL ? strlen(gr->gr_passwd) + 1 : 0); - numMem = 0; + /* Calculate size of the group. */ + len = sizeof(*gs); + if (gr->gr_name != NULL) + len += strlen(gr->gr_name) + 1; + if (gr->gr_passwd != NULL) + len += strlen(gr->gr_passwd) + 1; if (gr->gr_mem != NULL) { - for (; gr->gr_mem[numMem] != NULL; numMem++) - len += strlen(gr->gr_mem[numMem]) + 1; - len += (numMem + 1) * sizeof(*(gr->gr_mem)); - } + for (num_mem = 0; gr->gr_mem[num_mem] != NULL; num_mem++) + len += strlen(gr->gr_mem[num_mem]) + 1; + len += (num_mem + 1) * sizeof(*gr->gr_mem); + } else + num_mem = -1; /* Create new group and copy old group into it. */ - if ((ngr = calloc(1, len)) == NULL) + if ((gs = calloc(1, len)) == NULL) return (NULL); - len = sizeof(*ngr); - ngr->gr_gid = gr->gr_gid; + dst = (char *)&gs->members[num_mem + 1]; if (gr->gr_name != NULL) { - ngr->gr_name = (char *)ngr + len; - len += sprintf(ngr->gr_name, "%s", gr->gr_name) + 1; + gs->gr.gr_name = dst; + dst = stpcpy(gs->gr.gr_name, gr->gr_name) + 1; } if (gr->gr_passwd != NULL) { - ngr->gr_passwd = (char *)ngr + len; - len += sprintf(ngr->gr_passwd, "%s", gr->gr_passwd) + 1; + gs->gr.gr_passwd = dst; + dst = stpcpy(gs->gr.gr_passwd, gr->gr_passwd) + 1; } + gs->gr.gr_gid = gr->gr_gid; if (gr->gr_mem != NULL) { - ngr->gr_mem = (char **)((char *)ngr + len); - len += (numMem + 1) * sizeof(*(ngr->gr_mem)); - for (ndx = 0; gr->gr_mem[ndx] != NULL; ndx++) { - ngr->gr_mem[ndx] = (char *)ngr + len; - len += sprintf(ngr->gr_mem[ndx], "%s", - gr->gr_mem[ndx]) + 1; + gs->gr.gr_mem = gs->members; + for (ndx = 0; ndx < num_mem; ndx++) { + gs->gr.gr_mem[ndx] = dst; + dst = stpcpy(gs->gr.gr_mem[ndx], gr->gr_mem[ndx]) + 1; } - ngr->gr_mem[ndx] = NULL; + gs->gr.gr_mem[ndx] = NULL; } - return (ngr); + return (&gs->gr); } - /* * Scan a line and place it into a group structure. */ @@ -180,14 +194,14 @@ __gr_scan(char *line, struct group *gr) return (false); *loc = '\0'; gr->gr_passwd = loc + 1; - if (*(gr->gr_passwd) == ':') - *(gr->gr_passwd) = '\0'; + if (*gr->gr_passwd == ':') + *gr->gr_passwd = '\0'; else { if ((loc = strchr(loc + 1, ':')) == NULL) return (false); *loc = '\0'; } - if (sscanf(loc + 1, "%u", &(gr->gr_gid)) != 1) + if (sscanf(loc + 1, "%u", &gr->gr_gid) != 1) return (false); /* Assign member information to structure. */ @@ -195,40 +209,42 @@ __gr_scan(char *line, struct group *gr) return (false); line = loc + 1; gr->gr_mem = NULL; - if (*line != '\0') { - ndx = 0; + ndx = 0; + do { + gr->gr_mem = reallocf(gr->gr_mem, sizeof(*gr->gr_mem) * + (ndx + 1)); + if (gr->gr_mem == NULL) + return (false); + + /* Skip locations without members (i.e., empty string). */ do { - if ((gr->gr_mem = reallocf(gr->gr_mem, - sizeof(*(gr->gr_mem)) * (ndx + 1))) == NULL) - return (false); gr->gr_mem[ndx] = strsep(&line, ","); - } while (gr->gr_mem[ndx++] != NULL); - } + } while (gr->gr_mem[ndx] != NULL && *gr->gr_mem[ndx] == '\0'); + } while (gr->gr_mem[ndx++] != NULL); return (true); } - /* * Create a struct group from a line. */ struct group * gr_scan(const char *line) { - char *lineCopy; - struct group *newGr; struct group gr; + char *line_copy; + struct group *new_gr; - if ((lineCopy = strdup(line)) == NULL) + if ((line_copy = strdup(line)) == NULL) return (NULL); - if (!__gr_scan(lineCopy, &gr)) { - free(lineCopy); + if (!__gr_scan(line_copy, &gr)) { + free(line_copy); return (NULL); } - newGr = gr_dup(&gr); - free(lineCopy); + new_gr = gr_dup(&gr); + free(line_copy); if (gr.gr_mem != NULL) free(gr.gr_mem); - return (newGr); + return (new_gr); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200901080241.n082feiC006419>