ps: exploit enhanced library memory allocation provisions

There were numerous ps memory allocation inconsistencies.
Some were checked for failure and others were not.

The program was modified to utilize the library memory
rouines which are consistent in dealing with errors.

(a few changes simply removed trailing whitespace)
This commit is contained in:
Jim Warner 2011-11-22 09:36:00 -06:00 committed by Craig Small
parent 8fbe2dea04
commit 229be7b160
4 changed files with 46 additions and 52 deletions

View File

@ -29,6 +29,7 @@
#include <signal.h> /* catch signals */ #include <signal.h> /* catch signals */
#include "common.h" #include "common.h"
#include "../proc/alloc.h"
#include "../proc/wchan.h" #include "../proc/wchan.h"
#include "../proc/version.h" #include "../proc/version.h"
#include "../proc/readproc.h" #include "../proc/readproc.h"
@ -236,7 +237,7 @@ static void lists_and_needs(void){
format_node *p_end = &pfn; format_node *p_end = &pfn;
format_node *t_end = &tfn; format_node *t_end = &tfn;
while(walk){ while(walk){
format_node *new = malloc(sizeof(format_node)); format_node *new = xmalloc(sizeof(format_node));
memcpy(new,walk,sizeof(format_node)); memcpy(new,walk,sizeof(format_node));
p_end->next = walk; p_end->next = walk;
t_end->next = new; t_end->next = new;
@ -380,7 +381,7 @@ static void prep_forest_sort(void){
if(!sort_list) { /* assume start time order */ if(!sort_list) { /* assume start time order */
incoming = search_format_array("start_time"); incoming = search_format_array("start_time");
if(!incoming) { fprintf(stderr, "Could not find start_time!\n"); exit(1); } if(!incoming) { fprintf(stderr, "Could not find start_time!\n"); exit(1); }
tmp_list = malloc(sizeof(sort_node)); tmp_list = xmalloc(sizeof(sort_node));
tmp_list->reverse = 0; tmp_list->reverse = 0;
tmp_list->typecode = '?'; /* what was this for? */ tmp_list->typecode = '?'; /* what was this for? */
tmp_list->sr = incoming->sr; tmp_list->sr = incoming->sr;
@ -391,7 +392,7 @@ static void prep_forest_sort(void){
/* this is required for the forest option */ /* this is required for the forest option */
incoming = search_format_array("ppid"); incoming = search_format_array("ppid");
if(!incoming) { fprintf(stderr, "Could not find ppid!\n"); exit(1); } if(!incoming) { fprintf(stderr, "Could not find ppid!\n"); exit(1); }
tmp_list = malloc(sizeof(sort_node)); tmp_list = xmalloc(sizeof(sort_node));
tmp_list->reverse = 0; tmp_list->reverse = 0;
tmp_list->typecode = '?'; /* what was this for? */ tmp_list->typecode = '?'; /* what was this for? */
tmp_list->sr = incoming->sr; tmp_list->sr = incoming->sr;

View File

@ -20,10 +20,10 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include "common.h" #include "common.h"
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include "../proc/alloc.h"
#include "../proc/wchan.h" #include "../proc/wchan.h"
#include "../proc/version.h" #include "../proc/version.h"
#include "../proc/sysinfo.h" #include "../proc/sysinfo.h"
@ -234,7 +234,7 @@ static const char *set_personality(void){
if(sl > 15) return "Environment specified an unknown personality."; if(sl > 15) return "Environment specified an unknown personality.";
strncpy(buf, s, sl); strncpy(buf, s, sl);
buf[sl] = '\0'; buf[sl] = '\0';
saved_personality_text = strdup(buf); saved_personality_text = xstrdup(buf);
found = bsearch(&findme, personality_table, personality_table_count, found = bsearch(&findme, personality_table, personality_table_count,
sizeof(personality_table_struct), compare_personality_table_structs sizeof(personality_table_struct), compare_personality_table_structs

View File

@ -26,6 +26,7 @@
#include <unistd.h> #include <unistd.h>
#include "common.h" #include "common.h"
#include "../proc/alloc.h"
#include "../proc/version.h" #include "../proc/version.h"
#define ARG_GNU 0 #define ARG_GNU 0
@ -173,11 +174,10 @@ static const char *parse_list(const char *arg, const char *(*parse_fn)(char *, s
int need_item; int need_item;
const char *err; /* error code that could or did happen */ const char *err; /* error code that could or did happen */
/*** prepare to operate ***/ /*** prepare to operate ***/
node = malloc(sizeof(selection_node)); node = xmalloc(sizeof(selection_node));
node->u = malloc(strlen(arg)*sizeof(sel_union)); /* waste is insignificant */ node->u = xmalloc(strlen(arg)*sizeof(sel_union)); /* waste is insignificant */
node->n = 0; node->n = 0;
buf = malloc(strlen(arg)+1); buf = xstrdup(arg);
strcpy(buf, arg);
/*** sanity check and count items ***/ /*** sanity check and count items ***/
need_item = 1; /* true */ need_item = 1; /* true */
items = 0; items = 0;
@ -576,8 +576,8 @@ static const char *parse_bsd_option(void){
/* put our tty on a tiny list */ /* put our tty on a tiny list */
{ {
selection_node *node; selection_node *node;
node = malloc(sizeof(selection_node)); node = xmalloc(sizeof(selection_node));
node->u = malloc(sizeof(sel_union)); node->u = xmalloc(sizeof(sel_union));
node->u[0].tty = cached_tty; node->u[0].tty = cached_tty;
node->typecode = SEL_TTY; node->typecode = SEL_TTY;
node->n = 1; node->n = 1;
@ -705,8 +705,8 @@ static const char *parse_bsd_option(void){
if(!arg){ if(!arg){
/* Wow, obsolete BSD syntax. Put our tty on a tiny list. */ /* Wow, obsolete BSD syntax. Put our tty on a tiny list. */
selection_node *node; selection_node *node;
node = malloc(sizeof(selection_node)); node = xmalloc(sizeof(selection_node));
node->u = malloc(sizeof(sel_union)); node->u = xmalloc(sizeof(sel_union));
node->u[0].tty = cached_tty; node->u[0].tty = cached_tty;
node->typecode = SEL_TTY; node->typecode = SEL_TTY;
node->n = 1; node->n = 1;
@ -1018,16 +1018,16 @@ static const char *parse_trailing_pids(void){
argp = ps_argv + thisarg; argp = ps_argv + thisarg;
thisarg = ps_argc - 1; /* we must be at the end now */ thisarg = ps_argc - 1; /* we must be at the end now */
pidnode = malloc(sizeof(selection_node)); pidnode = xmalloc(sizeof(selection_node));
pidnode->u = malloc(i*sizeof(sel_union)); /* waste is insignificant */ pidnode->u = xmalloc(i*sizeof(sel_union)); /* waste is insignificant */
pidnode->n = 0; pidnode->n = 0;
grpnode = malloc(sizeof(selection_node)); grpnode = xmalloc(sizeof(selection_node));
grpnode->u = malloc(i*sizeof(sel_union)); /* waste is insignificant */ grpnode->u = xmalloc(i*sizeof(sel_union)); /* waste is insignificant */
grpnode->n = 0; grpnode->n = 0;
sidnode = malloc(sizeof(selection_node)); sidnode = xmalloc(sizeof(selection_node));
sidnode->u = malloc(i*sizeof(sel_union)); /* waste is insignificant */ sidnode->u = xmalloc(i*sizeof(sel_union)); /* waste is insignificant */
sidnode->n = 0; sidnode->n = 0;
while(i--){ while(i--){

View File

@ -18,6 +18,7 @@
#include <pwd.h> #include <pwd.h>
#include <grp.h> #include <grp.h>
#include "../proc/alloc.h"
#include "../proc/readproc.h" #include "../proc/readproc.h"
#include "../proc/sysinfo.h" #include "../proc/sysinfo.h"
#include "common.h" #include "common.h"
@ -38,7 +39,7 @@ static format_node *do_one_spec(const char *spec, const char *override){
if(fs){ if(fs){
int w1, w2; int w1, w2;
format_node *thisnode; format_node *thisnode;
thisnode = malloc(sizeof(format_node)); thisnode = xmalloc(sizeof(format_node));
if(fs->flags & CF_PIDMAX){ if(fs->flags & CF_PIDMAX){
w1 = (int)get_pid_digits(); w1 = (int)get_pid_digits();
w2 = strlen(fs->head); w2 = strlen(fs->head);
@ -49,12 +50,10 @@ static format_node *do_one_spec(const char *spec, const char *override){
if(override){ if(override){
w2 = strlen(override); w2 = strlen(override);
thisnode->width = (w1>w2)?w1:w2; thisnode->width = (w1>w2)?w1:w2;
thisnode->name = malloc(strlen(override)+1); thisnode->name = xstrdup(override);
strcpy(thisnode->name, override);
}else{ }else{
thisnode->width = w1; thisnode->width = w1;
thisnode->name = malloc(strlen(fs->head)+1); thisnode->name = xstrdup(fs->head);
strcpy(thisnode->name, fs->head);
} }
thisnode->pr = fs->pr; thisnode->pr = fs->pr;
thisnode->need = fs->need; thisnode->need = fs->need;
@ -144,8 +143,7 @@ static const char *aix_format_parse(sf_node *sfn){
} }
/*** sanity check passed ***/ /*** sanity check passed ***/
buf = malloc(strlen(sfn->sf)+1); buf = xstrdup(sfn->sf);
strcpy(buf, sfn->sf);
walk = sfn->sf; walk = sfn->sf;
while(items--){ while(items--){
@ -178,10 +176,9 @@ double_percent:
} }
buf[len] = '\0'; buf[len] = '\0';
walk += len; walk += len;
fnode = malloc(sizeof(format_node)); fnode = xmalloc(sizeof(format_node));
fnode->width = len; fnode->width = len;
fnode->name = malloc(len+1); fnode->name = xstrdup(buf);
strcpy(fnode->name, buf);
fnode->pr = NULL; /* checked for */ fnode->pr = NULL; /* checked for */
fnode->need = 0; fnode->need = 0;
fnode->vendor = AIX; fnode->vendor = AIX;
@ -215,8 +212,7 @@ static const char *format_parse(sf_node *sfn){
static char errbuf[80]; /* for variable-text error message */ static char errbuf[80]; /* for variable-text error message */
/*** prepare to operate ***/ /*** prepare to operate ***/
buf = malloc(strlen(sfn->sf)+1); buf = xstrdup(sfn->sf);
strcpy(buf, sfn->sf);
/*** sanity check and count items ***/ /*** sanity check and count items ***/
need_item = 1; /* true */ need_item = 1; /* true */
@ -331,7 +327,7 @@ static sort_node *do_one_sort_spec(const char *spec){
fs = search_format_array(spec); fs = search_format_array(spec);
if(fs){ if(fs){
sort_node *thisnode; sort_node *thisnode;
thisnode = malloc(sizeof(sort_node)); thisnode = xmalloc(sizeof(sort_node));
thisnode->sr = fs->sr; thisnode->sr = fs->sr;
thisnode->need = fs->need; thisnode->need = fs->need;
thisnode->reverse = reverse; thisnode->reverse = reverse;
@ -355,8 +351,7 @@ static const char *long_sort_parse(sf_node *sfn){
int need_item; int need_item;
/*** prepare to operate ***/ /*** prepare to operate ***/
buf = malloc(strlen(sfn->sf)+1); buf = xstrdup(sfn->sf);
strcpy(buf, sfn->sf);
/*** sanity check and count items ***/ /*** sanity check and count items ***/
need_item = 1; /* true */ need_item = 1; /* true */
@ -560,9 +555,8 @@ int defer_sf_option(const char *arg, int source){
const format_struct *fs; const format_struct *fs;
int need_item = 1; int need_item = 1;
sfn = malloc(sizeof(sf_node)); sfn = xmalloc(sizeof(sf_node));
sfn->sf = malloc(strlen(arg)+1); sfn->sf = xstrdup(arg);
strcpy(sfn->sf, arg);
sfn->sf_code = source; sfn->sf_code = source;
sfn->s_cooked = NULL; sfn->s_cooked = NULL;
sfn->f_cooked = NULL; sfn->f_cooked = NULL;
@ -685,10 +679,9 @@ static const char *generate_sysv_list(void){
if( (format_flags & FF_Ul) && !(format_modifiers & FM_y) ){ if( (format_flags & FF_Ul) && !(format_modifiers & FM_y) ){
if(personality & PER_IRIX_l){ /* add "rss" then ':' here */ if(personality & PER_IRIX_l){ /* add "rss" then ':' here */
PUSH("sgi_rss"); PUSH("sgi_rss");
fn = malloc(sizeof(format_node)); fn = xmalloc(sizeof(format_node));
fn->width = 1; fn->width = 1;
fn->name = malloc(2); fn->name = xstrdup(":");
strcpy(fn->name, ":");
fn->pr = NULL; /* checked for */ fn->pr = NULL; /* checked for */
fn->need = 0; fn->need = 0;
fn->vendor = AIX; /* yes, for SGI weirdness */ fn->vendor = AIX; /* yes, for SGI weirdness */