fix more of the pgrep memory-related brokenness

This commit is contained in:
albert 2006-06-18 23:19:30 +00:00
parent 5b13bb93c7
commit ffc32c6f8b

91
pgrep.c
View File

@ -7,7 +7,7 @@
* May be distributed under the conditions of the * May be distributed under the conditions of the
* GNU General Public License; a copy is in COPYING * GNU General Public License; a copy is in COPYING
* *
* Changes by Albert Cahalan, 2002. * Changes by Albert Cahalan, 2002,2006.
* *
*/ */
@ -81,37 +81,34 @@ split_list (const char *restrict str, int (*convert)(const char *, union el *))
char *copy = strdup (str); char *copy = strdup (str);
char *ptr = copy; char *ptr = copy;
char *sep_pos; char *sep_pos;
int i = 1, size = 32; int i = 0;
union el *list; int size = 0;
union el *list = NULL;
list = malloc (size * sizeof *list);
if (list == NULL)
exit (3);
do { do {
sep_pos = strchr (ptr, ',');
if (sep_pos)
*sep_pos = 0;
if (convert (ptr, &list[i]))
++i;
else
exit (2);
if (i == size) { if (i == size) {
size *= 2; size = size * 5 / 4 + 4;
list = realloc (list, size * sizeof *list); // add 1 because slot zero is a count
list = realloc (list, 1 + size * sizeof *list);
if (list == NULL) if (list == NULL)
exit (3); exit (3);
} }
sep_pos = strchr (ptr, ',');
if (sep_pos)
*sep_pos = 0;
// Use ++i instead of i++ because slot zero is a count
if (!convert (ptr, &list[++i]))
exit (2);
if (sep_pos) if (sep_pos)
ptr = sep_pos + 1; ptr = sep_pos + 1;
} while (sep_pos); } while (sep_pos);
free (copy); free (copy);
if (i == 1) { if (!i) {
free (list); free (list);
list = NULL; list = NULL;
} else { } else {
list[0].num = i - 1; list[0].num = i;
} }
return list; return list;
} }
@ -261,26 +258,28 @@ match_strlist (const char *restrict value, const union el *restrict list)
} }
static void static void
output_numlist (const union el *restrict list) output_numlist (const union el *restrict list, int num)
{ {
int i; int i;
for (i = 1; i < list[0].num; i++) const char *delim = opt_delim;
printf ("%ld%s", list[i].num, opt_delim); for (i = 0; i < num; i++) {
if(i+1==num)
if (list[0].num) delim = "\n";
printf ("%ld\n", list[i].num); printf ("%ld%s", list[i].num, delim);
}
} }
static void static void
output_strlist (const union el *restrict list) output_strlist (const union el *restrict list, int num)
{ {
// FIXME: escape codes // FIXME: escape codes
int i; int i;
for (i = 1; i < list[0].num; i++) const char *delim = opt_delim;
printf ("%s%s", list[i].str, opt_delim); for (i = 0; i < num; i++) {
if(i+1==num)
if (list[0].num) delim = "\n";
printf ("%s\n", list[i].str); printf ("%s%s", list[i].str, delim);
}
} }
static PROCTAB * static PROCTAB *
@ -354,26 +353,22 @@ select_procs (int *num)
unsigned long long saved_start_time; // for new/old support unsigned long long saved_start_time; // for new/old support
pid_t saved_pid = 0; // for new/old support pid_t saved_pid = 0; // for new/old support
int matches = 0; int matches = 0;
int size = 32; int size = 0;
regex_t *preg; regex_t *preg;
pid_t myself = getpid(); pid_t myself = getpid();
union el *list; union el *list = NULL;
char cmd[4096]; char cmd[4096];
list = malloc (size * sizeof *list); ptp = do_openproc();
if (list == NULL) preg = do_regcomp();
exit (3);
ptp = do_openproc ();
preg = do_regcomp ();
if (opt_newest) saved_start_time = 0ULL; if (opt_newest) saved_start_time = 0ULL;
if (opt_oldest) saved_start_time = ~0ULL; if (opt_oldest) saved_start_time = ~0ULL;
if (opt_newest) saved_pid = 0; if (opt_newest) saved_pid = 0;
if (opt_oldest) saved_pid = INT_MAX; if (opt_oldest) saved_pid = INT_MAX;
memset (&task, 0, sizeof (task)); memset(&task, 0, sizeof (task));
while (readproc (ptp, &task)) { while(readproc(ptp, &task)) {
int match = 1; int match = 1;
if (task.XXXID == myself) if (task.XXXID == myself)
@ -448,6 +443,12 @@ select_procs (int *num)
saved_pid = task.XXXID; saved_pid = task.XXXID;
matches = 0; matches = 0;
} }
if (matches == size) {
size = size * 5 / 4 + 4;
list = realloc(list, size * sizeof *list);
if (list == NULL)
exit (3);
}
if (opt_long) { if (opt_long) {
char buff[5096]; // FIXME char buff[5096]; // FIXME
sprintf (buff, "%d %s", task.XXXID, cmd); sprintf (buff, "%d %s", task.XXXID, cmd);
@ -455,12 +456,6 @@ select_procs (int *num)
} else { } else {
list[matches++].num = task.XXXID; list[matches++].num = task.XXXID;
} }
if (matches == size) {
size *= 2;
list = realloc(list, size * sizeof *list);
if (list == NULL)
exit (3);
}
} }
memset (&task, 0, sizeof (task)); memset (&task, 0, sizeof (task));
@ -645,9 +640,9 @@ main (int argc, char **argv)
} }
} else { } else {
if (opt_long) if (opt_long)
output_strlist (procs); output_strlist(procs,num);
else else
output_numlist (procs); output_numlist(procs,num);
} }
return !num; return !num;
} }