Page 1 of 1

SIGSEGV when running C++ Routine

Posted: Wed Sep 23, 2009 2:45 am
by BIuser
Hi Guys,

I am building a job that requires the use of a C++ routine (client request).

The C++ code is very simple, but it aborts with:

1)"Transformer_5,0: /bin/echo: write error: Broken pipe"
2)"Transformer_5,0: Operator terminated abnormally: received signal SIGSEGV"

It runs fine when only passing it 100 rows, but when I pass it more than 100 rows it aborts every time.

The C++ routines code is as follows:
-----------------------------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>


char outString[255];

char * dateHyphen(char *inString)
{
strcpy(outString, "");
if ( strlen(inString) < 8 ) {
strcpy(outString, inString);
} else {
strncpy(outString, inString, 4);
strcat(outString, "-");
strncat(outString, inString+4, 2);
strcat(outString, "-");
strncat(outString, inString+6, 2);
}
strcat(outString, "");
return outString;
}

---------------------------------------------------------------------------

I've searched DSXchange and consulted with other developers; but no resolution could be found.

Any help would be appreciated.

Thanks
:-)

Re: SIGSEGV when running C++ Routine

Posted: Wed Sep 23, 2009 3:59 am
by n.parameswara.reddy@accen
If the problem is in your
code, you will hit SIGSEGV even if you increase
'maxssiz'. In general, SIGSEGV can happen because
of 2 conditions, 1) illegal address being referenced
and 2) invalid permission for the address.

Posted: Wed Sep 23, 2009 4:42 am
by Sainath.Srinivasan
What is special after 100 rows ?

What is the data passed into inString ?

Pad your inString to some 10 characters with spaces (in the routine) and re-run.

Posted: Wed Sep 23, 2009 8:03 am
by nkabra
In your code, char outString[255]; as a global variable. So, it will allocate only upto 255 character. Now, till initial calls( say 100 rows), this function succeded because it has space but when it reaches 255 character and function will try to write something to array, it will be an array overflow exception. Hence, Segmentation fault occured i.e you are trying to read/write to a memory location which is not belongs to you.

As a solution, do not use this outString as global variable. Create it as local variable to function dateHyphen. So, everytime when the function gets called, this local variable will get memory allocated.

Just remember that local variable's scope is only till function. When you return from this function, your variable will be die.

So, I think it is good to have pointer instead of Array.

Here is you program which I have rewritten using pointers.

Please execute it and hope it will resolve your issue.

char * dateHyphenUsingPtr(char *inString)
{
char *outString = new char (12);
if ( strlen(inString) < 8 ) {
outString = inString;
}
else
{
int i = 0;
for (i = 0; i<4;i++)
{
*(outString + i) = *( inString + i);
}
*(outString + i) = '-';
i++;
for ( i; i<7;i++)
{
*( outString + i) = *(inString + i - 1);
}
*(outString + i) = '-';
i++;
for ( i; i<10;i++)
{
*( outString + i) = *(inString + i - 2);
}
*(outString + i) = '\0';
}

return outString;
}

Posted: Wed Sep 23, 2009 8:21 am
by Sainath.Srinivasan
Good effort nkabra.

But I differ from your point of "system is running out of memory".

In that case the total memory allocation will be 256 x 100 = 25600 (25Kb approx).

So this will imply that the system cannot even hold 25Kb in memory.

PS : The o/p is running V8 which will eat anything you give.

Posted: Wed Sep 23, 2009 11:40 pm
by nkabra
Sorry Srinath, I never said or mean that system is running out of memory. What I said that array is getting overflow. I am 100% sure on this.

You can increase the size of array and than execute it, It will definately process more than 100 rows.

Posted: Fri Sep 25, 2009 4:18 am
by BIuser
Hi Guys,

Thanks for the feedback!

I rewrote the routine and added a extra function to validate input data:
The "dateHyphen" routine now looks like this and it works, no matter what source data you pass it:

------------------------------------------------------------------------------
char * dateHyphen(char *inString, char *inString2) {


if (strcmp(dateValidation(inString, inString2), "VALID") == 0) {
outString[0] = inString[0];
outString[1] = inString[1];
outString[2] = inString[2];
outString[3] = inString[3];
outString[4] = '-';
outString[5] = inString[4];
outString[6] = inString[5];
outString[7] = '-';
outString[8] = inString[6];
outString[9] = inString[7];
outString[10] = 0;
return outString;
}

strcpy(outString, "");
return outString;
}
----------------------------------------------------------------------

I know it's clumsy code, but it works.

Thanks again for all you valuable input.

Cheers
:-)

Re: SIGSEGV when running C++ Routine

Posted: Thu Mar 25, 2010 3:36 am
by vasudev_koti
Check below ones you can eliminate SIGSEGV ERROR

1.Check MetaData
2.Recompile or reload the source program.