[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/2] hash, mgetgroups: drop xalloc dependency
From: |
Eric Blake |
Subject: |
[PATCH 1/2] hash, mgetgroups: drop xalloc dependency |
Date: |
Tue, 26 Apr 2011 22:22:10 -0600 |
Merely including xalloc.h can drag in some unwanted link
dependencies on some platforms. safe-alloc.c is already
one example of a file that broke the cycle by duplicating
the xalloc_oversized macro.
* lib/hash.c (hash_oversized): Copy from xalloc.h.
* lib/mgetgroups.c (mgetgroups_oversized): Likewise.
(xgetgroups): Move...
* lib/xgetgroups.c: ...to new file.
* modules/xgetgroups: New file, split from...
* modules/mgetgroups: ...here.
* modules/hash (Depends-on): Drop xalloc.
Signed-off-by: Eric Blake <address@hidden>
---
Perhaps an alternate approach would be to list just Files: of
xalloc.h, along with a preprocessor macro that a .c file can
define to get just the xalloc_oversized macro without also
getting the inline definitions of functions that pull in a
link dependency on xalloc_die.
ChangeLog | 9 +++++++++
lib/hash.c | 21 +++++++++++++++++++--
lib/mgetgroups.c | 33 ++++++++++++++++++++-------------
lib/xgetgroups.c | 37 +++++++++++++++++++++++++++++++++++++
modules/hash | 3 +--
modules/mgetgroups | 1 -
modules/xgetgroups | 24 ++++++++++++++++++++++++
7 files changed, 110 insertions(+), 18 deletions(-)
create mode 100644 lib/xgetgroups.c
create mode 100644 modules/xgetgroups
diff --git a/ChangeLog b/ChangeLog
index c75e8f4..a610358 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
2011-04-26 Eric Blake <address@hidden>
+ hash, mgetgroups: drop xalloc dependency
+ * lib/hash.c (hash_oversized): Copy from xalloc.h.
+ * lib/mgetgroups.c (mgetgroups_oversized): Likewise.
+ (xgetgroups): Move...
+ * lib/xgetgroups.c: ...to new file.
+ * modules/xgetgroups: New file, split from...
+ * modules/mgetgroups: ...here.
+ * modules/hash (Depends-on): Drop xalloc.
+
fchdir: avoid extra chdir and fix test
* modules/fchdir (Depends-on): Add dosname, filenamecat-lgpl,
getcwd-lgpl.
diff --git a/lib/hash.c b/lib/hash.c
index f3de2aa..1efdb6c 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -27,7 +27,6 @@
#include "hash.h"
#include "bitrotate.h"
-#include "xalloc.h"
#include <stdint.h>
#include <stdio.h>
@@ -43,6 +42,24 @@
# endif
#endif
+/* Return 1 if an array of N objects, each of size S, cannot exist due
+ to size arithmetic overflow. S must be positive and N must be
+ nonnegative. This is a macro, not an inline function, so that it
+ works correctly even when SIZE_MAX < N.
+
+ By gnulib convention, SIZE_MAX represents overflow in size
+ calculations, so the conservative dividend to use here is
+ SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
+ However, malloc (SIZE_MAX) fails on all known hosts where
+ sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
+ exactly-SIZE_MAX allocations on such hosts; this avoids a test and
+ branch when S is known to be 1.
+
+ This is the same as xalloc_oversized from xalloc.h
+*/
+#define hash_oversized(n, s) \
+ ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
+
struct hash_entry
{
void *data;
@@ -551,7 +568,7 @@ compute_bucket_size (size_t candidate, const Hash_tuning
*tuning)
candidate = new_candidate;
}
candidate = next_prime (candidate);
- if (xalloc_oversized (candidate, sizeof (struct hash_entry *)))
+ if (hash_oversized (candidate, sizeof (struct hash_entry *)))
return 0;
return candidate;
}
diff --git a/lib/mgetgroups.c b/lib/mgetgroups.c
index 5c79915..77ab542 100644
--- a/lib/mgetgroups.c
+++ b/lib/mgetgroups.c
@@ -31,12 +31,30 @@
#endif
#include "getugroups.h"
-#include "xalloc.h"
+
+/* Return 1 if an array of N objects, each of size S, cannot exist due
+ to size arithmetic overflow. S must be positive and N must be
+ nonnegative. This is a macro, not an inline function, so that it
+ works correctly even when SIZE_MAX < N.
+
+ By gnulib convention, SIZE_MAX represents overflow in size
+ calculations, so the conservative dividend to use here is
+ SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
+ However, malloc (SIZE_MAX) fails on all known hosts where
+ sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
+ exactly-SIZE_MAX allocations on such hosts; this avoids a test and
+ branch when S is known to be 1.
+
+ This is the same as xalloc_oversized from xalloc.h
+*/
+#define mgetgroups_oversized(n, s) \
+ ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
+
static gid_t *
realloc_groupbuf (gid_t *g, size_t num)
{
- if (xalloc_oversized (num, sizeof *g))
+ if (mgetgroups_oversized (num, sizeof *g))
{
errno = ENOMEM;
return NULL;
@@ -193,14 +211,3 @@ mgetgroups (char const *username, gid_t gid, gid_t
**groups)
return ng;
}
-
-/* Like mgetgroups, but call xalloc_die on allocation failure. */
-
-int
-xgetgroups (char const *username, gid_t gid, gid_t **groups)
-{
- int result = mgetgroups (username, gid, groups);
- if (result == -1 && errno == ENOMEM)
- xalloc_die ();
- return result;
-}
diff --git a/lib/xgetgroups.c b/lib/xgetgroups.c
new file mode 100644
index 0000000..41886c9
--- /dev/null
+++ b/lib/xgetgroups.c
@@ -0,0 +1,37 @@
+/* xgetgroups.c -- return a list of the groups a user or current process is in
+
+ Copyright (C) 2007-2011 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Extracted from coreutils' src/id.c. */
+
+#include <config.h>
+
+#include "mgetgroups.h"
+
+#include <errno.h>
+
+#include "xalloc.h"
+
+/* Like mgetgroups, but call xalloc_die on allocation failure. */
+
+int
+xgetgroups (char const *username, gid_t gid, gid_t **groups)
+{
+ int result = mgetgroups (username, gid, groups);
+ if (result == -1 && errno == ENOMEM)
+ xalloc_die ();
+ return result;
+}
diff --git a/modules/hash b/modules/hash
index 75a99da..6ebca01 100644
--- a/modules/hash
+++ b/modules/hash
@@ -1,5 +1,5 @@
Description:
-Parametrizable hash table.
+Parameterizable hash table.
Files:
lib/hash.c
@@ -10,7 +10,6 @@ Depends-on:
bitrotate
stdbool
stdint
-xalloc
configure.ac:
gl_HASH
diff --git a/modules/mgetgroups b/modules/mgetgroups
index c404d01..eeb525c 100644
--- a/modules/mgetgroups
+++ b/modules/mgetgroups
@@ -10,7 +10,6 @@ Depends-on:
getgroups
getugroups
realloc-gnu
-xalloc
configure.ac:
gl_MGETGROUPS
diff --git a/modules/xgetgroups b/modules/xgetgroups
new file mode 100644
index 0000000..0236a87
--- /dev/null
+++ b/modules/xgetgroups
@@ -0,0 +1,24 @@
+Description:
+Return the group IDs of a user or current process in malloc'd storage, with
+out-of-memory checking.
+
+Files:
+lib/xgetgroups.c
+
+Depends-on:
+mgetgroups
+xalloc
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += xgetgroups.c
+
+Include:
+"mgetgroups.h"
+
+License:
+GPL
+
+Maintainer:
+Jim Meyering, Eric Blake
--
1.7.4.4
- [PATCH 1/2] hash, mgetgroups: drop xalloc dependency,
Eric Blake <=