[PATCH] strace (main): Check calloc return value

Dmitry V. Levin ldv at altlinux.org
Mon Oct 8 21:08:26 UTC 2007


OK, commited as obvious.

On Wed, Sep 26, 2007 at 08:32:04AM +0800, Bai Weidong wrote:
> On Tue, Sep 25, 2007 at 05:26:13PM +0400, Dmitry V. Levin wrote:
> > On Tue, Sep 25, 2007 at 09:13:09AM +0800, Bai Weidong wrote:
> > hello:
> >     I am afraid that strace may need minor changes on strace 
> > allocating memory failed.
> 
> >Here is a test case for linux/i686:
> >$ ulimit -v 6144 && strace `perl -e "print \"'' \" x 32700"`
> >Segmentation fault
> 
> >I propose a bit different patch to fix this.
> .
>        Thank you for your reviewing very much. Your patch is right.
> 
> >--- a/strace/strace.c
> >+++ b/strace/strace.c
> >@@ -345,9 +345,7 @@ newoutf(struct tcb *tcp)
> > }
>  
> > int
> >-main(argc, argv)
> >-int argc;
> >-char *argv[];
> >+main(int argc, char *argv[])
> > {
> >  extern int optind;
> >  extern char *optarg;
> >@@ -357,14 +355,21 @@ char *argv[];
> > 
> >  static char buf[BUFSIZ];
>  >
> >+ progname = argv[0] ? argv[0] : "strace";
> >+
> >  /* Allocate the initial tcbtab.  */
> >  tcbtabsize = argc; /* Surely enough for all -p args.  */
> >- tcbtab = (struct tcb **) malloc (tcbtabsize * sizeof tcbtab[0]);
> >- tcbtab[0] = (struct tcb *) calloc (tcbtabsize, sizeof *tcbtab[0]);
> >+ if ((tcbtab = calloc (tcbtabsize, sizeof tcbtab[0])) == NULL) {
> >+  fprintf(stderr, "%s: out of memory\n", progname);
> >+  exit(1);
> >+ }
> >+ if ((tcbtab[0] = calloc (tcbtabsize, sizeof tcbtab[0][0])) == NULL) {
> >+  fprintf(stderr, "%s: out of memory\n", progname);
> >+  exit(1);
> >+ }
> >  for (tcp = tcbtab[0]; tcp < &tcbtab[0][tcbtabsize]; ++tcp)
>  >  tcbtab[tcp - tcbtab[0]] = &tcbtab[0][tcp - tcbtab[0]];
> > 
> >- progname = argv[0];
> > outf = stderr;
> >  interactive = 1;
> >  set_sortby(DEFAULT_SORTBY);


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20071009/63d98d04/attachment.bin>


More information about the Strace-devel mailing list