SIGSEGV when running C++ Routine
Moderators: chulett, rschirm, roy
SIGSEGV when running C++ Routine
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
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
https://www.ssa.co.za
-
- Participant
- Posts: 40
- Joined: Mon May 18, 2009 5:22 am
Re: SIGSEGV when running C++ Routine
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.
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.
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
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;
}
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;
}
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
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
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
https://www.ssa.co.za
-
- Participant
- Posts: 3
- Joined: Sat Oct 25, 2008 5:39 am
Re: SIGSEGV when running C++ Routine
Check below ones you can eliminate SIGSEGV ERROR
1.Check MetaData
2.Recompile or reload the source program.
1.Check MetaData
2.Recompile or reload the source program.