From: Benjamin Kaduk Date: Thu, 30 Aug 2018 14:54:23 +0000 (-0500) Subject: ptserver: move allocation out of put_prentries() into listEntries() X-Git-Tag: openafs-devel-1_9_1~101 X-Git-Url: http://git.openafs.org/?p=openafs.git;a=commitdiff_plain;h=c1c4e308cfe0b189778259840f02183c83c1083e ptserver: move allocation out of put_prentries() into listEntries() put_prentries() is a helper function for listEntries(), but the contract between the two is rather odd -- put_prentries() is expected to notice when the backing store has not yet been allocated and silently allocate it, even though there is only the single caller and the allocation could be done in the caller. Move the allocation to the caller and adjust the "buffer is full" logic accordingly, and normalize the initialization of the output array to just use calloc() instead of individual memset()s when populating each entry. Change-Id: Icf84e3b60eae81a1570b12d7adbf006a24a104f3 Reviewed-on: https://gerrit.openafs.org/13315 Reviewed-by: Michael Meffie Reviewed-by: Benjamin Kaduk Tested-by: BuildBot --- diff --git a/src/ptserver/ptprocs.c b/src/ptserver/ptprocs.c index 5eb1ac7..90416ab 100644 --- a/src/ptserver/ptprocs.c +++ b/src/ptserver/ptprocs.c @@ -110,7 +110,7 @@ static afs_int32 listEntry(struct rx_call *call, afs_int32 aid, static afs_int32 listEntries(struct rx_call *call, afs_int32 flag, afs_int32 startindex, prentries *bulkentries, afs_int32 *nextstartindex, afs_int32 *cid); -static afs_int32 put_prentries(struct prentry *tentry, prentries *bulkentries); +static void put_prentries(struct prentry *tentry, prentries *bulkentries); static afs_int32 changeEntry(struct rx_call *call, afs_int32 aid, char *name, afs_int32 oid, afs_int32 newid, afs_int32 *cid); static afs_int32 setFieldsEntry(struct rx_call *call, afs_int32 id, @@ -1440,6 +1440,7 @@ SPR_ListEntries(struct rx_call *call, afs_int32 flag, afs_int32 startindex, return code; } +#define PR_MAXENTRIES 500 static afs_int32 listEntries(struct rx_call *call, afs_int32 flag, afs_int32 startindex, prentries *bulkentries, afs_int32 *nextstartindex, afs_int32 *cid) @@ -1470,6 +1471,12 @@ listEntries(struct rx_call *call, afs_int32 flag, afs_int32 startindex, eof = ntohl(cheader.eofPtr) - sizeof(cheader); maxentries = eof / sizeof(struct prentry); + + bulkentries->prentries_val = calloc(PR_MAXENTRIES, + sizeof(bulkentries->prentries_val[0])); + if (!bulkentries->prentries_val) + ABORT_WITH(tt, PRNOMEM); + for (i = startindex; i < maxentries; i++) { pos = i * sizeof(struct prentry) + sizeof(cheader); code = pr_ReadEntry(tt, 0, pos, &tentry); @@ -1486,11 +1493,9 @@ listEntries(struct rx_call *call, afs_int32 flag, afs_int32 startindex, f = (tentry.flags & PRTYPE); if (((flag & PRUSERS) && (f == 0)) || /* User entry */ ((flag & PRGROUPS) && (f & PRGRP))) { /* Group entry */ - code = put_prentries(&tentry, bulkentries); - if (code == -1) - break; /* Filled return array */ - if (code) - goto done; + put_prentries(&tentry, bulkentries); + if (bulkentries->prentries_len >= PR_MAXENTRIES) + break; } } code = 0; @@ -1512,29 +1517,14 @@ listEntries(struct rx_call *call, afs_int32 flag, afs_int32 startindex, return PRSUCCESS; } -#define PR_MAXENTRIES 500 -static afs_int32 +static void put_prentries(struct prentry *tentry, prentries *bulkentries) { struct prlistentries *entry; - if (bulkentries->prentries_val == 0) { - bulkentries->prentries_len = 0; - bulkentries->prentries_val = malloc(PR_MAXENTRIES * - sizeof(struct prlistentries)); - if (!bulkentries->prentries_val) { - return (PRNOMEM); - } - } - - if (bulkentries->prentries_len >= PR_MAXENTRIES) { - return (-1); - } - entry = bulkentries->prentries_val; entry += bulkentries->prentries_len; - memset(entry, 0, sizeof(*entry)); entry->flags = tentry->flags >> PRIVATE_SHIFT; if (entry->flags == 0) { entry->flags = @@ -1550,7 +1540,6 @@ put_prentries(struct prentry *tentry, prentries *bulkentries) entry->count = tentry->count; strncpy(entry->name, tentry->name, PR_MAXNAMELEN); bulkentries->prentries_len++; - return 0; } afs_int32