Re: [xml] Segmentation fault - xmlNewDoc



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 <mailto:xml_user2003 yahoo com>  

To: xml gnome org <mailto: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
<http://shopping.yahoo.com/?__yltc=s%3A150000443%2Cd%3A22708228%2Cslk%3Atext
%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


The information in this message is intended solely for the addressee and should be considered confidential. VISTA does not accept legal responsibility for the contents of this message and any statements contained herein which do not relate to the official business of VISTA are neither given nor endorsed by VISTA and are those of the individual and not of VISTA. This message has been scanned for viruses using the most current and reliable tools available and VISTA excludes all liability related to any viruses that might exist in any attachment or which may have been acquired in transit.


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