summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2018-08-15 12:01:34 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2018-08-15 12:01:34 (GMT)
commit988b8e27f5df26d7e9f6fd7984677873ca1c40cb (patch) (side-by-side diff)
tree15aabc0bc27950487d0377e13577d9e877eaf62c
parent082c967151db85d4ad551fa458c48fc7a783f446 (diff)
downloadpam-modules-988b8e27f5df26d7e9f6fd7984677873ca1c40cb.tar.gz
pam-modules-988b8e27f5df26d7e9f6fd7984677873ca1c40cb.tar.bz2
Bugfix
* pam_groupmember/pam_groupmember.c (split): Use standard memory allocation functions. Correctly handle empty strings (demimiter immediately followed by another delimiter.
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--pam_groupmember/pam_groupmember.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/pam_groupmember/pam_groupmember.c b/pam_groupmember/pam_groupmember.c
index ba418d0..7045d13 100644
--- a/pam_groupmember/pam_groupmember.c
+++ b/pam_groupmember/pam_groupmember.c
@@ -57,30 +57,45 @@ struct pam_opt pam_opt[] = {
static char **
split(const char *str, int delim)
{
- const char *p;
- size_t c = 1, i;
+ size_t i = 1;
+ char const *p;
char **v;
+ i = 2;
for (p = str; *p; p++)
if (*p == delim)
- ++c;
+ ++i;
- ++c;
+ v = calloc(i, sizeof(*v));
+ if (v) {
+ i = 0;
+ p = str;
- v = gray_calloc(c, sizeof(*v));
-
- for (p = str, i = 0; *p; p++) {
- if (*p == delim) {
+ while (1) {
+ if (*p == delim || *p == 0) {
size_t len = p - str;
- char *elt = gray_malloc(len + 1);
+ char *elt = malloc(len + 1);
+ if (!elt) {
+ int ec = errno;
+ while (i > 0) {
+ free(v[i-1]);
+ --i;
+ }
+ free(v);
+ errno = ec;
+ return NULL;
+ }
memcpy(elt, str, len);
elt[len] = 0;
v[i++] = elt;
+ if (*p == 0)
+ break;
str = ++p;
+ } else
+ ++p;
}
- }
- v[i++] = gray_strdup(str);
v[i] = 0;
+ }
return v;
}

Return to:

Send suggestions and report system problems to the System administrator.