[Info-vax] calloc fails with access violation

chrisj.doran at proemail.co.uk chrisj.doran at proemail.co.uk
Mon Aug 24 14:52:58 EDT 2009


On 24 Aug, 20:28, Jose Cuevas <jcue... at mac.com> wrote:
> On Aug 24, 1:45 pm, VAXman-  @SendSpamHere.ORG wrote:
>
>
>
> > In article <aecec9c9-3fe0-4b76-b37a-c5cd9ebf5... at a13g2000yqc.googlegroups.com>, Jose Cuevas <jcue... at mac.com> writes:
>
> > >1. We have a problem with a fairly simple library written in C that
> > >gets called from Cobol.
>
> > >We are getting an access violation (a crash) on calls to calloc() of
> > >insignificant size. While at first we thought it was on a particular
> > >record we then found that it is a far more inconsistent.
>
> > >The C code runs fine when tested on a unix machine.
>
> 1. The code fails randomly at different points that use calloc. For
> example at the C function.
>
> char *ckGetCobolString(char *sIn){
>         int i = 0;
>         char *s = calloc(strlen(sIn), sizeof(char));

The above needs to allocate strlen(sIn) + 1 chars because

>         strcpy(s, sIn);

copies in the '\0' too, maybe corrupting the control struct which
precedes the following entry in the heap. You get away with it
sometimes because heap is usually allocated in word-sized blocks, so
you get a bit more than you asked for. But when strlen takes you to
the end of the last char allocated, the NUL is then over the boundary.

The control struct may be different and less susceptible on your Unix
machine.

Chris



More information about the Info-vax mailing list