Re: [xml] Segmentation fault - xmlNewDoc



Hi,
Thanks for your reply. I did the size of uid to userName+1. Also my replace char does reallocate the buffer size to accomodate the additional chars. Still the behavior I explained remains....the first time everything works fine, when I try to invoke constructMessage the second time, I get a segmentation fault. If I do not replace any chars in the xml string generated by the libxml toolkit, the program doesn't crash.
 
 
BTW Here is my code for replaceChar()
 
#define LINESIZE 4096
void replaceChar(char** data,char* oldChar,char* newChar)
{
  int newStringIndex;
  char* nextIndex;
  int currIndex;
  char newString[LINESIZE];
  if((nextIndex=(char *)strstr(*data,oldChar))==NULL)
    return;
  currIndex=0;
  newStringIndex = 0;
  while(nextIndex!=NULL)
    {
      strncpy(newString+newStringIndex,*data+currIndex,(nextIndex-*data)-currInd
ex);
      newStringIndex+=(nextIndex-*data)-currIndex;
      strncpy(newString+newStringIndex,newChar,strlen(newChar));
      newStringIndex+=strlen(newChar);
      currIndex = (nextIndex-*data)+strlen(oldChar);
      nextIndex = strstr(*data+currIndex,oldChar);
    }
  strcpy(newString+newStringIndex,*data+currIndex);
  *data ="">  strcpy(*data,newString);
  free(newString);
  free(nextIndex);
}


Mark Itzcovitz <mark itzcovitz ntlworld com> wrote:
Also xmlDocDumpMemory has told you how big your Message buffer is and then you make it bigger by translating spaces to %20 (unless of course replaceChar re-allocates the buffer, but you don't show that so I don't know).
> _____
>
> From: Ed Day [mailto:eday obj-sys com]
> Sent: 07 October 2003 15:55
> To: XML User; xml gnome org
> Subject: Re: [xml] Segmentation fault - xmlNewDoc
>
>
>
> > Am I missing something?
>
>
>
> Sure, you are only allocating strlen chars for your uid. You need strlen +
> 1 to account for the null terminator char if you are using strcpy.
>
>
>
> Regards,
>
>
>
> Ed Day
>
>
>
> ----- Original Message -----
>
> From: XML User
>
> To: xml gnome org
>
> Sent: Tuesday, October 07, 2003 10:24 AM
>
> Subject: [xml] Segmentation fault - xmlNewDoc
>
>
>
> Hi,
>
> I am using libXml to create an xml message and pass on the message to a
> webserver. Since I am sending the message as part of a GET request, I remove
> all the new line characters from the message. For this I am using a small
> helper function which removes all new line characters from the message. My
> problem is that the program works fine the first time, however if I try to
> construct another xml message I get a segmentation fault. If I do not do any
> newline character replacements within the xml string, everything works fine.
>
>
>
> Here is the function I am using
>
>
>
> char* constructMessage(const char *userName,
> const char* responsibility)
> {
> xmlDocPtr doc;
> xmlNodePtr tree, subtree,subsubtree;
> xmlChar* Message;
> int size;
> char* uid;
>
> uid = (char*)malloc(strlen(userName)*sizeof(char));
> strcpy(uid,userName);
> encodeData(&uid);
>
> /*construct Message */
> doc = xmlNewDoc("1.0");
> doc->children = xmlNewDocNode(doc, NULL, "Message", NULL);
> xmlSetProp(doc->children,"MessageId","");
> xmlSetProp(doc->children,"IntObjectFormat","Hierarchical");
> xmlSetProp(doc->children,"MessageType","Integration Object");
> xmlSetProp(doc->children,"IntObjectName","User Registration");
> tree = xmlNewChild(doc->children, NULL, "Start_UserRegistration", NULL)
> subtree = xmlNewChild(tree, NULL, "UserRegistration", NULL);
> subsubtree = xmlNewChild(subtree, NULL, "FirstName", uid);
> subsubtree = xmlNewChild(subtree, NULL, "LastName", uid);
> subsubtree = xmlNewChild(subtree, NULL, "LoginName", uid);
> subsubtree = xmlNewChild(subtree, NULL,
> "ListOfUserRegistration_Responsibilit
> ", NULL);
> tree = subsubtree;
> subtree = xmlNewChild(tree, NULL, "UserRegistration_Responsibility",
> NULL);
> subsubtree = xmlNewChild(subtree, NULL, "Responsibility", responsibility);
> xmlDocDumpMemory(doc,&Message,&size);
>
> /*helper function*/
>
> replaceChar(&Message,"\n","");
> replaceChar(&Message," ","%20");
>
> xmlFreeDoc(doc);
> free(uid);
> return (char*)Message;
> }
>
>
>
> Am I missing something?
>
>
>
> Thanks,
>
> Akshay.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _____
>
>
> Do you Yahoo!?
> The
> > %2Csec%3Amail> New Yahoo! Shopping - with improved product search


-----------------------------------------
Email provided by http://www.ntlhome.com/

 

 


From: Ed Day [mailto:eday obj-sys com]
Sent: 07 October 2003 15:55
To: XML User; xml gnome org
Subject: Re: [xml] Segmentation fault - xmlNewDoc

 

> Am I missing something?

 

Sure, you are only allocating strlen chars for your uid.  You need strlen + 1 to account for the null terminator char if you are using strcpy.

 

Regards,

 

Ed Day

 

----- Original Message -----

From: XML User

Sent: Tuesday, October 07, 2003 10:24 AM

Subject: [xml] Segmentation fault - xmlNewDoc

 

Hi,

I am using libXml to create an xml message and pass on the message to a webserver. Since I am sending the message as part of a GET request, I remove all the new line characters from the message. For this I am using a small helper function which removes all new line characters from the message. My problem is that the program works fine the first time, however if I try to construct another xml message I get a segmentation fault. If I do not do any newline character replacements within the xml string, everything works fine.

 

Here is the function I am using

 

char* constructMessage(const char *userName,
                                  const char* responsibility)
{
  xmlDocPtr doc;
  xmlNodePtr tree, subtree,subsubtree;
  xmlChar* Message;
  int size;
  char* uid;

  uid = (char*)malloc(strlen(userName)*sizeof(char));
  strcpy(uid,userName);
  encodeData(&uid);

  /*construct Message */
  doc = xmlNewDoc("1.0");
  doc->children = xmlNewDocNode(doc, NULL, "Message", NULL);
  xmlSetProp(doc->children,"MessageId","");
  xmlSetProp(doc->children,"IntObjectFormat","Hierarchical");
  xmlSetProp(doc->children,"MessageType","Integration Object");
  xmlSetProp(doc->children,"IntObjectName","User Registration");
  tree = xmlNewChild(doc->children, NULL, "Start_UserRegistration", NULL)
  subtree = xmlNewChild(tree, NULL, "UserRegistration", NULL);
  subsubtree = xmlNewChild(subtree, NULL, "FirstName", uid);
  subsubtree = xmlNewChild(subtree, NULL, "LastName", uid);
  subsubtree = xmlNewChild(subtree, NULL, "LoginName", uid);
  subsubtree = xmlNewChild(subtree, NULL, "ListOfUserRegistration_Responsibilit
", NULL);
  tree = subsubtree;
  subtree = xmlNewChild(tree, NULL, "UserRegistration_Responsibility", NULL);
  subsubtree = xmlNewChild(subtree, NULL, "Responsibility", responsibility);
  xmlDocDumpMemory(doc,&Message,&size);

/*helper function*/

 replaceChar(&Message,"\n","");
 replaceChar(&Message," ","%20");

  xmlFreeDoc(doc);
  free(uid);
  return (char*)Message;
}

 

Am I missing something?

 

Thanks,

Akshay.

 

 

 

 

 

 

 

 


Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]