[xml] How to properly free a string allocated by xmlCharStrdup

2013-12-26 Thread Shlomi Fish
Hi all,

happy holidays and a happy new (civil) year.

I'm the maintainer of XML::LibXSLT which are the CPAN bindings for libxslt.
Now, someone reported that the clang compiler emits some warnings, and part of
the problem was that I passed 1.0 to xmlNewDoc instead of its equivalent
xmlChar *. So I did:

xmlChar * xml_ver = NULL;
xml_ver = xmlCharStrdup(1.0);
xmlNewDoc(xml_ver)

// At the function FINISH:
free( xml_ver );

However, now it was reported that the Win32 tests generate a segfault:

https://rt.cpan.org/Ticket/Display.html?id=91652

So I tried to replace free() by xmlMemFree() but now I'm getting errors upon
make test on Linux:

[QUOTE SHELL]

shlomif[XML-LibXML]:$xslt_trunk$ hg di
diff -r 0290d4243e9b LibXSLT.xs
--- a/LibXSLT.xsSun Dec 22 18:23:22 2013 +0200
+++ b/LibXSLT.xsThu Dec 26 12:15:35 2013 +0200
@@ -249,7 +249,7 @@
 
 XPUSHs(perl_function);
 
-xml_ver = xmlCharStrdup(1.0);
+   xml_ver = xmlCharStrdup(1.0);
/* clone all of the arguments into a new owning document */
owner_doc = x_PmmNodeToSv(INT2PTR(xmlNodePtr,xmlNewDoc(xml_ver)), NULL);
XPUSHs( sv_2mortal(owner_doc) );
@@ -428,11 +428,11 @@
 ret = (xmlXPathObjectPtr)xmlXPathNewCString(SvPV(perl_result, len));
 
 FINISH:
-free( xml_ver );
+xmlMemFree( xml_ver );
 valuePush(ctxt, ret);
 PUTBACK;
 FREETMPS;
-LEAVE;
+LEAVE;
 }
 
 /*
shlomif[XML-LibXML]:$xslt_trunk$ perl Mak
Makefile  Makefile.old  Makefile.PL   Makefile.PL~  
shlomif[XML-LibXML]:$xslt_trunk$ perl Makefile.PL
running xslt-config... ok
looking for -lxslt... yes
looking for -lexslt... yes
running pkg-config libexslt... ok
Warning (mostly harmless): No library found for -ldb
Generating a GNU-style Makefile
Writing Makefile for XML::LibXSLT
Writing MYMETA.yml and MYMETA.json
shlomif[XML-LibXML]:$xslt_trunk$ make
Skip blib/lib/XML/LibXSLT.pm (unchanged)
gcc -c  -I/usr/include/libxml2 -I/usr/include/libxml2  -D_REENTRANT
-D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -g -pipe -Wformat
-Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector
--param=ssp-buffer-size=4 -fPIC -Wl,--as-needed -Wl,-z,relro -Wl,-O1
-Wl,--build-id -Wl,--enable-new-dtags -pthread   -DVERSION=\1.83\
-DXS_VERSION=\1.83\ -fPIC
-I/usr/lib/perl5/5.18.1/x86_64-linux-thread-multi/CORE  -DHAVE_BLANK
-DHAVE_BLANK -DHAVE_EXSLT LibXSLT.c gcc -c  -I/usr/include/libxml2
-I/usr/include/libxml2  -D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe
-fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64 -O2 -g -pipe -Wformat -Werror=format-security
-Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -fPIC
-Wl,--as-needed -Wl,-z,relro -Wl,-O1 -Wl,--build-id -Wl,--enable-new-dtags
-pthread   -DVERSION=\1.83\ -DXS_VERSION=\1.83\ -fPIC
-I/usr/lib/perl5/5.18.1/x86_64-linux-thread-multi/CORE  -DHAVE_BLANK
-DHAVE_BLANK -DHAVE_EXSLT perl-libxml-mm.c Running Mkbootstrap for XML::LibXSLT
() chmod 644 LibXSLT.bs rm -f blib/arch/auto/XML/LibXSLT/LibXSLT.so
LD_RUN_PATH=/usr/lib64 gcc  -shared -O2 -g -pipe -Wformat
-Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector
--param=ssp-buffer-size=4 -fPIC -Wl,--as-needed -Wl,-z,relro -Wl,-O1
-Wl,--build-id -Wl,--enable-new-dtags -pthread -L/usr/local/lib64 LibXSLT.o
perl-libxml-mm.o  -o blib/arch/auto/XML/LibXSLT/LibXSLT.so   \ -L/usr/lib64
-lxslt -lxml2 -lz -ldl -lm -lexslt -ldl -lgcrypt -lgpg-error -lnsl -lgdbm -ldl
-lm -lcrypt -lutil -lpthread -lc -lgdbm_compat   \ chmod 755
blib/arch/auto/XML/LibXSLT/LibXSLT.so cp LibXSLT.bs
blib/arch/auto/XML/LibXSLT/LibXSLT.bs chmod 644
blib/arch/auto/XML/LibXSLT/LibXSLT.bs Manifying blib/man3/XML::LibXSLT.3pm
shlomif[XML-LibXML]:$xslt_trunk$ make test
PERL_DL_NONLAZY=1 /usr/bin/perl5.18.1 -MExtUtils::Command::MM
-MTest::Harness -e undef *Test::Harness::Switches; test_harness(0,
'blib/lib', 'blib/arch') t/*.t t/01basic.t .. 1/4 

Compiled against:libxslt 10128, libxml2 20901
Running: libxslt 10128, libxml2 20901
Compiled with EXSLT: yes

t/01basic.t .. ok   
t/02dromeds.t  ok   
t/03input.t .. ok 
t/04params.t . ok   
t/05quick.t .. ok 
t/06entities.t ... ok   
t/06output.t . ok 
t/07blankdoc.t ... ok   
t/08literal.t  ok   
t/09exslt.t .. ok   
t/10functions.t .. 1/38 Memory tag error occurs :1d89a78 
 bye
xmlMemFree(1D89AA0) error
xmlMallocBreakpoint reached on block 0
Memory tag error occurs :1d898b8 
 bye
xmlMemFree(1D898E0) error
xmlMallocBreakpoint reached on block 0
Memory tag error occurs :1d89ce8 
 bye
xmlMemFree(1D89D10) error
xmlMallocBreakpoint reached on block 0
Memory tag error occurs :1d89f28 
 bye
xmlMemFree(1D89F50) error
xmlMallocBreakpoint reached on block 0
Memory tag error occurs :1d8a6e8 
 bye
xmlMemFree(1D8A710) error
xmlMallocBreakpoint reached on block 0
Memory tag error occurs :1d8a278 
   

Re: [xml] How to properly free a string allocated by xmlCharStrdup

2013-12-26 Thread Nikolay Sivov

On 12/26/2013 14:19, Shlomi Fish wrote:

Hi all,

happy holidays and a happy new (civil) year.

I'm the maintainer of XML::LibXSLT which are the CPAN bindings for libxslt.
Now, someone reported that the clang compiler emits some warnings, and part of
the problem was that I passed 1.0 to xmlNewDoc instead of its equivalent
xmlChar *. So I did:

xmlChar * xml_ver = NULL;
xml_ver = xmlCharStrdup(1.0);
xmlNewDoc(xml_ver)

// At the function FINISH:
free( xml_ver );

Use xmlFree(). But for this particular case you don't need to allocate 
anything, just do xmlNewDoc(NULL).


How can I properly free a string allocated by xmlCharStrdup? The documentation
is lacking on details (and, if you ask me, should be made more explicit.).

You got all the code, for that case it's pretty simple.


Regards,

Shlomi Fish



___
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
https://mail.gnome.org/mailman/listinfo/xml


Re: [xml] How to properly free a string allocated by xmlCharStrdup

2013-12-26 Thread Shlomi Fish
Hi Nikolay,

On Thu, 26 Dec 2013 14:58:27 +0400
Nikolay Sivov bungleh...@gmail.com wrote:

 On 12/26/2013 14:19, Shlomi Fish wrote:
  Hi all,
 
  happy holidays and a happy new (civil) year.
 
  I'm the maintainer of XML::LibXSLT which are the CPAN bindings for libxslt.
  Now, someone reported that the clang compiler emits some warnings, and part
  of the problem was that I passed 1.0 to xmlNewDoc instead of its
  equivalent xmlChar *. So I did:
 
  xmlChar * xml_ver = NULL;
  xml_ver = xmlCharStrdup(1.0);
  xmlNewDoc(xml_ver)
 
  // At the function FINISH:
  free( xml_ver );
 
 Use xmlFree(). But for this particular case you don't need to allocate 
 anything, just do xmlNewDoc(NULL).

Thanks! Hopefully it will work well. I credited you in the Changes file.

 
  How can I properly free a string allocated by xmlCharStrdup? The
  documentation is lacking on details (and, if you ask me, should be made
  more explicit.).
 You got all the code, for that case it's pretty simple.

Well, the fact is that I got it wrong. And twice. Also see what Joel on
Software wrote about fixing every problem two ways here:

http://www.joelonsoftware.com/articles/customerservice.html

Just for reference I refer to
http://xmlsoft.org/html/libxml-xmlstring.html#xmlCharStrdup which says it is «a
strdup for char's to xmlChar's» and does not tell one how to free it (in fact
the string «xmlFree» does not appear anywhere on the page), which prompted me
to ask the question after getting a segmentation fault for that on Windows.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Freecell Solver - http://fc-solve.shlomifish.org/

I HAVE NEITHER BEEN THERE NOR DONE THAT
— Bart Simpson on chalkboard in episode AABF17

Please reply to list if it's a mailing list post - http://shlom.in/reply .
___
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
https://mail.gnome.org/mailman/listinfo/xml