General
cancel
Showing results for 
Search instead for 
Did you mean: 

Segmentation error with string assignment in RHEL5.5

Sarada
Frequent Visitor

Segmentation error with string assignment in RHEL5.5

My application is failing with segmentation violation in lunux RHEL5.5 platform.

The gdb trace is as below -


                                RA_INFOTRACE("Rules Getting Fetched for %s and Compare %s",elem->ruleName,elem->cmpName);
                                //string eventFld = ruleTable.getEventFieldValue(elem->ruleName, elem->cmpName);
                                char* eventFld = ruleTable.getEventFieldValue(elem->ruleName, elem->cmpName);
                                RA_LOG_USER_INFO("printing eventFld1")
                               // RA_LOG_USER_INFO(eventFld.c_str())
                                cout<<"printing eventFld2"<<  eventFld <<endl;
                                cout<<"printing elem->subsid" << elem->subsid << endl;
                                RA_LOG_USER_INFO("printing elem->subsid")
                                RA_LOG_USER_INFO(elem->subsid)
                                //strcpy(elem->subsid , eventFld.c_str());
                                strcpy(elem->subsid , eventFld);
Program received signal SIGSEGV, Segmentation fault.
0x0000003ed38792a0 in strcpy () from /lib64/libc.so.6
(gdb) bt
#0  0x0000003ed38792a0 in strcpy () from /lib64/libc.so.6
#1  0x000000000041167c in generateCERuleList(char*, ListIterator<cleanseRules, cleanseRules>*, int, int, char**) ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

While checing the code , we observed the below assignment to string variable "eventFld" is failing though the function getEventFieldValue() is returning the correct string value ( return by value).
And then the process is crashing in strcpy function.

                 string eventFld = ruleTable.getEventFieldValue(elem->ruleName, elem->cmpName);
                 strcpy(elem->subsid , eventFld.c_str());

We tried to use char* type instead of string for the variable eventFld and also chnaged in getEventFieldValue() function to return char* , then the process ran successfully.

   char* eventFld = ruleTable.getEventFieldValue(elem->ruleName, elem->cmpName);
                 strcpy(elem->subsid , eventFld);


 Note that the same old code was working properly and string operation ( mentioned above) was happening accurately in RHEL4.5. We have seeing this issue only in RHEL5.5.
Whetehr we have to apply any OS patch ??


Do you have any suggestion on this.

5 REPLIES
Dennis Handly
Acclaimed Contributor

Re: Segmentation error with string assignment in RHEL5.5

>While checking the code, we observed the below assignment to string variable "eventFld" is failing though the function getEventFieldValue() is returning the correct string value (return by value).

 

Failing how?  What happens when you print out eventFld?

Any reason you don't return by const ref?

 

>And then the process is crashing in strcpy function.

>                 strcpy(elem->subsid, eventFld.c_str());

 

Can you print out the hex address of: eventFld.c_str()

 

>We tried to use char* type instead of string for the variable eventFld and also changed in getEventFieldValue() function to >return char* , then the process ran successfully.

 

If you are just looking at the field, that may be more efficient than a string.

Sarada
Frequent Visitor

Re: Segmentation error with string assignment in RHEL5.5

No value printed While printing the eventFld after the below assignment. but the value returned from

getEventFieldValue() is a string.

 

string eventFld = ruleTable.getEventFieldValue(elem->ruleName, elem->cmpName);

Dennis Handly
Acclaimed Contributor

Re: Segmentation error with string assignment in RHEL5.5

>No value printed while printing the eventFld after the below assignment.

 

What is the exact output?  Do you get any better output for other strings?

Sarada
Frequent Visitor

Re: Segmentation error with string assignment in RHEL5.5

Hi,

   The ouput returned for eventFld  after the assignment and the address of  eventFld.c_str() is also below.

=======

eventFld value 1:
HEx address :0x7fff7724ff90

========

I tried to print o/p of other string used in a function , and found the value is printed properly.

 

I am mentioning the defination of function getEventFieldValue() as below and also the structure pointed by elem .

 

struct cleanseRules
{
   char ruleName[50];
   char cmpName[10];
   int promote_seq;
   int correct_seq;
   char status[10];
   char subsid[30];
};

cleanseRules *elem = 0;
CHECK_ALLOCATION(elem = new cleanseRules);

string RaCleanseTable::getEventFieldValue(char *ruleName_in, char *cmpName_in)
{
   EXEC SQL BEGIN DECLARE SECTION;
     char rule_name[50];
     char cmp_name[10];
                 char subsid_out[50];
   EXEC SQL END DECLARE SECTION;
         RA_ENTERFUNC("%s RaCleanseTable::getEventFieldValue ","");
         memset(rule_name,'\0',sizeof(rule_name));
         memset(cmp_name,'\0',sizeof(rule_name));
         memset(subsid_out,'\0',sizeof(subsid_out));

         strcpy(rule_name, ruleName_in);
         strcpy(cmp_name, cmpName_in);

         RA_INFOTRACE("Rule Name %s and Compare Name %s ",rule_name,cmp_name);

         try {
                         EXEC SQL SELECT SUBSID INTO :subsid_out FROM RA_CLEANSE_RULES WHERE
                                CMP_NAME = :cmp_name and RULE_NAME = :rule_name;
      DifUtilities::checkDatabaseError();
         }
         catch ( NoMoreRows & nmr)
         {
                                return string("");
   }
         RETHROW("updateRuleTable","getEventFieldValue-01")
   DifUtilities::trimStr(subsid_out);

         string subsid_ret = subsid_out
         RA_INFOTRACE("EVENT Key field from the cleanse rule table is %s. ",subsid_ret.c_str());
         RA_ENTERFUNC("%s RaCleanseTable::getEventFieldValue ","");
         return subsid_ret;
}

 

Dennis Handly
Acclaimed Contributor

Re: Segmentation error with string assignment in RHEL5.5

>The output returned for eventFld  after the assignment and the address of  eventFld.c_str() is also below.

 

Have you tried printing the content of that hex address?

When I print the string directly, it shows _M_dataplus and _M_p field, the later contains the chars.