<div dir="ltr"><span style="font-size:12.8px">Oh, please sorry. ^^' I don't know why my e-mail client sometimes send the message to a particular e-mail instead of the mail-list. o.O</span><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 20, 2017 at 12:10 AM, Aleksey Sanin <span dir="ltr"><<a href="mailto:aleksey@aleksey.com" target="_blank">aleksey@aleksey.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Great, you might want to re-post it to the <a href="mailto:xmlsec@aleksey.com">xmlsec@aleksey.com</a> mailing<br>
list so people can find this post in the future.<br>
<br>
Aleksey<br>
<br>
On 1/19/17 8:14 AM, silvioprog wrote:<br>
> Finally fixed this problem using an object as error handler! If someone<br>
> want to take a look at the final code, it was sent to: <a href="https://git.io/vMDNR" rel="noreferrer" target="_blank">https://git.io/vMDNR</a>.<br>
><br>
> Thanks! :-)<br>
><br>
> On Thu, Jan 19, 2017 at 11:12 AM, silvioprog <<a href="mailto:silvioprog@gmail.com">silvioprog@gmail.com</a><br>
> <mailto:<a href="mailto:silvioprog@gmail.com">silvioprog@gmail.com</a>>> wrote:<br>
><br>
>     Attached example that reproduces the problem. Notice commenting the<br>
>     line "#define DAMN_SIGILL 1".<br>
><br>
>     To compile and run:<br>
><br>
>     $ cmake . && make && ./sigsegv<br>
><br>
>     On Thu, Jan 19, 2017 at 1:10 AM, silvioprog <<a href="mailto:silvioprog@gmail.com">silvioprog@gmail.com</a><br>
>     <mailto:<a href="mailto:silvioprog@gmail.com">silvioprog@gmail.com</a>>> wrote:<br>
><br>
>         It seems the problem is related to nested function's life cycle:<br>
><br>
>         1. my `extern mxml_xml_init()` set the `_error_callback()`<br>
>         nested reference function to `xmlSetGenericErrorFunc()`;<br>
>         2. my `mxml_xml_validate_file()` calls the<br>
>         `xmlSchemaValidateFile()`, but the `_error_callback()` was freed<br>
>         by the `mxml_xml_init()` scope, raising the SIGSEGV.<br>
><br>
>         But I'm not absolutely sure about it anyway.<br>
><br>
>         Is there functions like `xmlSetGenericErrorFunc()`, however,<br>
>         using a `va_list` parameter instead off "..." `varargs`?<br>
><br>
>         On Wed, Jan 18, 2017 at 9:19 PM, silvioprog<br>
>         <<a href="mailto:silvioprog@gmail.com">silvioprog@gmail.com</a> <mailto:<a href="mailto:silvioprog@gmail.com">silvioprog@gmail.com</a>>> wrote:<br>
><br>
>             Indeed. I forgot to show my environment too:<br>
><br>
>             Xubuntu 16.04 64 bits<br>
>             gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609<br>
>             libxml-2.0 - 2.9.3 (installed via apt-get)<br>
>             xmlsec1 - 1.2.20 (installed via apt-get)<br>
><br>
>             Unfortunately the GBD log[1] doesn't show any precise info<br>
>             about the error. I notice that commenting the line<br>
>             "xmlSetGenericErrorFunc(error_<wbr>cls, &_error_callback);" the<br>
>             sigsegv stops, however I need to call my own callback. I<br>
>             tested declaring the "_error_callback" outside function too,<br>
>             like:<br>
><br>
>             void _error_callback(void *cls, const char *fmt, ...) {<br>
>                 char *error;<br>
>                 char msg[MXML_ERROR_MAX_SIZE];<br>
>             //    if (NULL != error_cb) {<br>
>                     va_list va;<br>
>                     va_start(va, fmt);<br>
>                     vasprintf(&error, fmt, va);<br>
>                     strcpy(msg, error);<br>
>                     free(error);<br>
>                     va_end(va);<br>
>             //        error_cb(error_cls, msg);<br>
>             //    }<br>
>             }<br>
><br>
>             void mxml_xml_init(mxml_error_<wbr>callback error_cb, void<br>
>             *error_cls) {<br>
>                 xmlInitParser();<br>
>                 xmlSetGenericErrorFunc(error_<wbr>cls, &_error_callback);<br>
>             }<br>
><br>
>             and the problem was solved, but notice commented lines, in<br>
>             that way I can't use my "error_cb" anymore. :-(<br>
><br>
>             And the sigsegv happen when my library call the function<br>
>             "xmlSchemaSetValidErrors(cfg-><wbr>ctxt, &_error_callback,<br>
>             &_warn_callback, error_warn_cls);".<br>
><br>
>             [1]<br>
>             GNU gdb (GDB) 7.11.1<br>
>             Copyright (C) 2016 Free Software Foundation, Inc.<br>
>             License GPLv3+: GNU GPL version 3 or later<br>
>             <<a href="http://gnu.org/licenses/gpl.html" rel="noreferrer" target="_blank">http://gnu.org/licenses/gpl.<wbr>html</a><br>
>             <<a href="http://gnu.org/licenses/gpl.html" rel="noreferrer" target="_blank">http://gnu.org/licenses/gpl.<wbr>html</a>>><br>
>             This is free software: you are free to change and<br>
>             redistribute it.<br>
>             There is NO WARRANTY, to the extent permitted by law.  Type<br>
>             "show copying"<br>
>             and "show warranty" for details.<br>
>             This GDB was configured as "x86_64-pc-linux-gnu".<br>
>             Type "show configuration" for configuration details.<br>
>             For bug reporting instructions, please see:<br>
>             <<a href="http://www.gnu.org/software/gdb/bugs/" rel="noreferrer" target="_blank">http://www.gnu.org/software/<wbr>gdb/bugs/</a><br>
>             <<a href="http://www.gnu.org/software/gdb/bugs/" rel="noreferrer" target="_blank">http://www.gnu.org/software/<wbr>gdb/bugs/</a>>>.<br>
>             Find the GDB manual and other documentation resources online at:<br>
>             <<a href="http://www.gnu.org/software/gdb/documentation/" rel="noreferrer" target="_blank">http://www.gnu.org/software/<wbr>gdb/documentation/</a><br>
>             <<a href="http://www.gnu.org/software/gdb/documentation/" rel="noreferrer" target="_blank">http://www.gnu.org/software/<wbr>gdb/documentation/</a>>>.<br>
>             For help, type "help".<br>
>             Type "apropos word" to search for commands related to "word".<br>
>             No source file named<br>
>             /home/silvioprog/dev/git/ws/<wbr>libmicroxml/src/microxml.c.<br>
>             [Thread debugging using libthread_db enabled]<br>
>             Using host libthread_db library<br>
>             "/lib/x86_64-linux-gnu/<wbr>libthread_db.so.1".<br>
><br>
>             Breakpoint 1, main (argc=3, argv=0x7fffffffe158) at<br>
>             /home/silvioprog/dev/git/ws/<wbr>libmicroxml/examples/c/<wbr>validate.c:19<br>
>             19   if (3 != argc) {<br>
><br>
>             Breakpoint 2, mxml_xml_init (error_cb=0x400a06<br>
>             <error_callback>) at<br>
>             /home/silvioprog/dev/git/ws/<wbr>libmicroxml/src/microxml.c:85<br>
>             85   xmlSetGenericErrorFunc(error_<wbr>cls, &_error_callback);<br>
><br>
>             Program received signal SIGSEGV, Segmentation fault.<br>
>             0x00007fffffffdf10 in ?? ()<br>
><br>
>             On Wed, Jan 18, 2017 at 8:20 PM, Aleksey Sanin<br>
>             <<a href="mailto:aleksey@aleksey.com">aleksey@aleksey.com</a> <mailto:<a href="mailto:aleksey@aleksey.com">aleksey@aleksey.com</a>>> wrote:<br>
><br>
>                 You probably want to get a precise location of sigsegv<br>
>                 with gdb.<br>
><br>
>                 Aleksey<br>
><br>
>                 On 1/18/17 2:49 PM, silvioprog wrote:<br>
>                 > Hello masters,<br>
>                 ><br>
>                 > I'm trying to use the following code in a library that<br>
>                 extends<br>
>                 > libxml2/xmlsec1:<br>
>                 ><br>
>                 > ...<br>
>                 ><br>
>                 > typedef void (*mxml_error_callback)(void *cls, const<br>
>                 char *msg);<br>
>                 ><br>
>                 > ...<br>
>                 ><br>
>                 > void mxml_xml_init(mxml_error_<wbr>callback error_cb, void<br>
>                 *error_cls) {<br>
>                 ><br>
>                 >     void _error_callback(void *cls, const char *fmt,<br>
>                 ...) {<br>
>                 >         char *error;<br>
>                 >         char msg[MXML_ERROR_MAX_SIZE];<br>
>                 >         if (NULL != error_cb) {<br>
>                 >             va_list va;<br>
>                 >             va_start(va, fmt);<br>
>                 >             vasprintf(&error, fmt, va);<br>
>                 >             strcpy(msg, error);<br>
>                 >             free(error);<br>
>                 >             va_end(va);<br>
>                 >             error_cb(error_cls, msg);<br>
>                 >         }<br>
>                 >     }<br>
>                 ><br>
>                 >     xmlInitParser();<br>
>                 >     xmlSetGenericErrorFunc(error_<wbr>cls, &_error_callback);<br>
>                 > }<br>
>                 ><br>
>                 > ...<br>
>                 ><br>
>                 > However, when I run my project, I got a "interrupted<br>
>                 by signal 11:<br>
>                 > SIGSEGV", even commenting the entire local code, eg:<br>
>                 ><br>
>                 > ...<br>
>                 ><br>
>                 > void mxml_xml_init(mxml_error_<wbr>callback error_cb, void<br>
>                 *error_cls) {<br>
>                 >     void _error_callback(void *cls, const char *fmt,<br>
>                 ...) {<br>
>                 >     }<br>
>                 >     xmlInitParser();<br>
>                 >     xmlSetGenericErrorFunc(error_<wbr>cls, &_error_callback);<br>
>                 > }<br>
>                 ><br>
>                 > ...<br>
>                 ><br>
>                 > Unfortunately, I need to publish something like<br>
>                 `mxml_error_callback`<br>
>                 > instead of default `xmlGenericErrorFunc` because some<br>
>                 languages (eg:<br>
>                 > Pascal) doesn't offer any feature to get the<br>
>                 parameters of a `varargs`<br>
>                 > function, so I want pass only a "const char *msg" to them.<br>
>                 ><br>
>                 > I have other declarations like this that causes<br>
>                 SIGSEGV too:<br>
>                 ><br>
>                 > ...<br>
>                 ><br>
>                 > struct mxml_xml_cfg *mxml_xml_cfg_new(const char *xsd_uri,<br>
>                 ><br>
>                  mxml_error_callback error_cb,<br>
>                 ><br>
>                  mxml_error_callback warn_cb, void<br>
>                 > *error_warn_cls) {<br>
>                 ><br>
>                 >     void _error_callback(void *cls, const char *fmt,<br>
>                 ...) {<br>
>                 > ... code ...<br>
>                 >     }<br>
>                 ><br>
>                 >     void _warn_callback(void *cls, const char *fmt, ...) {<br>
>                 > ... code ...<br>
>                 >     }<br>
>                 ><br>
>                 > ... code ...<br>
>                 > ... code ...<br>
>                 > ... code ...<br>
>                 >     xmlSchemaSetParserErrors(cfg-><wbr>parser,<br>
>                 &_error_callback,<br>
>                 > &_warn_callback, error_warn_cls);<br>
>                 ><br>
>                 > ...<br>
>                 ><br>
>                 > I can't understand why it doesn't work. It seems my<br>
>                 local function has<br>
>                 > the same signature of `xmlGenericErrorFunc` function,<br>
>                 but... :-(<br>
>                 ><br>
>                 > A snip of the main test:<br>
>                 ><br>
>                 > ...<br>
>                 ><br>
>                 > static void error_callback(void *cls, const char *msg) {<br>
>                 >     fprintf(stderr, "%s", msg);<br>
>                 > }<br>
>                 ><br>
>                 > int main() {<br>
>                 > ...<br>
>                 >     mxml_xml_init(&error_callback, NULL);<br>
>                 ><br>
>                 > ...<br>
>                 ><br>
>                 > Thank you!<br>
>                 ><br>
>                 > --<br>
>                 > Silvio Clécio<br>
><br>
><br>
>             --<br>
>             Silvio Clécio<br>
><br>
><br>
>         --<br>
>         Silvio Clécio<br>
><br>
><br>
>     --<br>
>     Silvio Clécio<br>
><br>
<span class="gmail-HOEnZb"><font color="#888888">><br>
> --<br>
> Silvio Clécio<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Silvio Clécio</div></div></div>
</div></div>