Subject: Problematic code bit..

From: Nathan Bates <[email-masked]>
Date: Tue, 06 Jan 1998 10:48:19 -0500 (EST)

I'm having troubles with the following code bit:

        /*
        ** Begin code bit..
        */

        ENVELOPE *header = NIL;
        BODY *body = NIL;
        int mesgs = 0, mesgno = 0;

        mesgs = mstream->nmsgs;
        while (mesgs > mesgno) {
                char *cHeader = (char *) malloc(sizeof cHeader);
                if (cHeader) return NIL;

                printf("--> Message %d\n", mesgno);

                header = mail_fetchstructure(mstream,mesgno,&body);

                /*
                ** Next line causes mail_fetchstructure to fail on
                ** next incantation.
                */

                rfc822_header(cHeader, header, body);

                puts(cHeader);
                puts("------------------------------------------------\n");

                free(cHeader);
                mesgno++;
        }

        return T;

        /*
        ** End code bit.
        */

I feel as though body is getting clobbered, but I don't seem to track it
down.  I'm using imap-4.1-BETA.tar.gz provided c-client.

Any assistance is greatly appreciated.  If there is any other information
that I need to provide, please ask.

        Regards,
        Nathan Bates

Nathan Brock Bates <[email-masked]> internetMCI iNOC <[email-masked]>
-------------------------------------------------------------------------
The opinions expressed herein do not express those of internetMCI or MCI.

---

From: Karsten Ballueder <[email-masked]>
Date: Tue, 6 Jan 1998 16:02:20 GMT

>                 char *cHeader = (char *) malloc(sizeof cHeader);

cHeader is a char pointer, so sizeof cHeader should be 1. Then malloc
will allocate just one byte.
Also, if malloc was successfull, you leave the function.

This doesn't seem to make any sense.
(Am I missing something, or is there a typo?)
-- 
Karsten Ballüder http://Ballueder.home.ml.org/ mailto:[email-masked]
Physics Department, Heriot-Watt University, Edinburgh,EH14 4AS,Scotland
             "In a world without fences, who needs Gates?"

---

From: Mark Crispin <[email-masked]>
Date: Tue, 6 Jan 1998 08:39:54 -0800 (PST)

>                 char *cHeader = (char *) malloc(sizeof cHeader);

This is quite bogus.  As written, the code seems to be intended to print out
an RFC822 representation of the results of an RFC822/MIME parse.  If you just
wanted to print out an RFC822 header, mail_fetch_header() (possibly with some
line filters) may be a better choice.

If you really do want to parse then deparse, then you must allocate sufficient
space for "cHeader".  Either you run down the ENVELOPE and BODY structures to
calculate the needed sizes (duplicating what rfc822_header() does), or you
have to make cHeader arbitrarily large and hope that it's large enough.

>                 if (cHeader) return NIL;

This returns from the function if the malloc() succeeded (presumably you
wanted !cHeader instead of cHeader here).

---

From: Nathan Bates <[email-masked]>
Date: Tue, 06 Jan 1998 11:46:27 -0500 (EST)

Oops.. that was a type when I was typing it in to make sense..  :)

                char *cHeader = (char *) malloc(sizeof (char *));
                if (!cHeader) return NIL;

The second incantation of mail_fetchstructure() still fails.

        Regards,
        Nathan Bates

On 06-Jan-98 Karsten Ballueder wrote:
> Karsten Ball|der http://Ballueder.home.ml.org/ mailto:[email-masked]

---

From: Nathan Bates <[email-masked]>
Date: Tue, 06 Jan 1998 11:57:27 -0500 (EST)

Mark,

Well, like I said, it is just a code snippet.  My purpose is a bit
different than that.  Either way, I was just reproducing the error.

My intention was to create a dynmically allocated memory area.

        char *cHeader = (char *) malloc(sizeof(char *));

As for the second, you are correct in saying that I wanted !cHeader.
I should have just cut the code I had rather than reduping by
keystrokes it and trying to make it look sensible..

        Thank you,
        Nathan

---

From: Mark Crispin <[email-masked]>
Date: Tue, 6 Jan 1998 09:25:55 -0800 (PST)

>         char *cHeader = (char *) malloc(sizeof(char *));

sizeof(char *) is 4 bytes on most systems.

In other words, that statement allocates a buffer of 4 bytes, and a pointer to
it is assigned to routine cHeader.  That is sufficient space to store a 3
character null-terminated string.

Since rfc822_header() typically writes about 1000 bytes (sometimes less, often
more), it will overflow the buffer, and then write into other areas of the
malloc() arena.

This is a C programming issue, not a c-client issue.  If you do not understand
this, I recommend that you get ahold of a good introductory text that explains
C programming.

---

From: Nathan Bates <[email-masked]>
Date: Tue, 06 Jan 1998 16:28:16 -0500 (EST)

I understand.  I see my flaw.

        Thank you,
        Nathan