SIGSEGV when running C++ Routine

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
BIuser
Premium Member
Premium Member
Posts: 238
Joined: Thu Feb 02, 2006 4:03 am
Location: South Africa

SIGSEGV when running C++ Routine

Post 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
:-)
-------------------------
https://www.ssa.co.za
n.parameswara.reddy@accen
Participant
Posts: 40
Joined: Mon May 18, 2009 5:22 am

Re: SIGSEGV when running C++ Routine

Post 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.
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post 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.
nkabra
Participant
Posts: 3
Joined: Sat Aug 22, 2009 3:15 am

Post 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;
}
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post 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.
nkabra
Participant
Posts: 3
Joined: Sat Aug 22, 2009 3:15 am

Post 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.
BIuser
Premium Member
Premium Member
Posts: 238
Joined: Thu Feb 02, 2006 4:03 am
Location: South Africa

Post 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
:-)
-------------------------
https://www.ssa.co.za
vasudev_koti
Participant
Posts: 3
Joined: Sat Oct 25, 2008 5:39 am

Re: SIGSEGV when running C++ Routine

Post by vasudev_koti »

Check below ones you can eliminate SIGSEGV ERROR

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