Tuesday, 17 September 2013

c program aborts with core dump unless printf is inserted in code?

c program aborts with core dump unless printf is inserted in code?

I have a function that aborts with a core dump unless I insert a printf:
// Read all available text from the connection
char *sslRead (connection *c)
{
const int readSize = 1024;
char *rc = NULL;
int received, count = 0;
char buffer[1024];
// printf("??"); // If I comment this out: Aborted (core dumped)
if (c)
{
while (1)
{
if (!rc)
rc = malloc (readSize * sizeof (char) + 1);
else
rc = realloc (rc, (count + 1) *
readSize * sizeof (char) + 1);
received = SSL_read (c->sslHandle, buffer, readSize);
buffer[received] = '\0';
if (received > 0)
strcat (rc, buffer);
if (received < readSize)
break;
count++;
}
}
return rc;
}
The malloc seems to be the offending line.
The full source code is here:
http://savetheions.com/2010/01/16/quickly-using-openssl-in-c/
What could be causing this?
Below is the output from my build:
23:06:41 **** Incremental Build of configuration Debug for project
HelloWorldOpenSSL ****
Info: Internal Builder is used for build
gcc "-IC:\\dev\\cygwin64\\opt\\cs\\include" -O0 -g3 -Wall -c
-fmessage-length=0 -o MyC.o "..\\MyC.c"
gcc "-LC:\\dev\\cygwin64\\opt\\cs\\lib" -o HelloWorldOpenSSL.exe MyC.o
-lssl -lcrypto
23:06:42 Build Finished (took 804ms)

No comments:

Post a Comment