in reply to [Ceph::RADOS] Help Debugging Inline C

First, continue with your posted C. Using Newx (a malloc) will just leak the memory.
Second: put a debugging fprintf(stderr, "rados_pool_list()=%d\n", buf_sz); after the first rados_pool_list() call.

What does it print? A reasonable positive size? (I assume your C compiler is C99 capable; if not, you can rewrite the char buf[buf_sz]; as char *buf = alloca(buf_sz);).

Now, if rados_pool_list() returned a negative (error) value, try passing it "" in stead of NULL.
A quick glance at librados.cc makes me think there may be a bug there.

Oooomh, a quick update: another thing you should do is replace

  int buf_sz;
with a
  size_t buf_sz;
This, unfortunately will not improve the situation, since rados_pool_list() is prototyped as int, where it really ought to be ssize_t rados_pool_list(). File a ticket with the project?

Replies are listed 'Best First'.
Re^2: [Ceph::RADOS] Help Debugging Inline C
by three18ti (Monk) on Nov 09, 2013 at 22:36 UTC

    Awesome! Thanks for this.

    The first thing I did was add the debugging string, which returned a negative number. Thanks for pointing out where in the original code the negative number was coming from.

    Ultimately, replacing the NULL with "" did the trick and I no longer get a segfault!

    Any thoughts on moving Inline_Stack_Vars to the beginning of the sub definition?

    That sounds like something that would be worth filing a bug report about. What is your reasoning behind the prototyping?

    Thanks for your help. If you have any more, general advice on writing a Perl wrapper around a C API I'd love to hear it. This is my first foray into trying to extend a C library so I've got a bit of learning to do :)

    Thanks again for your help!

      Regarding rados library and the rados_pool_list() — seeing that code instantly reminded me of snprintf.

      The snprintf/vsnprintf functions return n. of chars that would have been written with large enough buffer. The length argument is size_t, whereas return type is int... (Historical stdio matter?) SUSv2 and C99 are in disagreement concerning a snprintf call with len==0; with C99 allowing buf==NULL in that case.

      The ssize_t type allows a -1 error return where a size_t would otherwise be suitable. Like e.g. read() or pwrite.

      hi I am looking forward the "perl library" for RADOS. could you share the code with me. thanks
        Hi guys. I just did a search and noticed people were actually using the proof of concept code I dumped. I have committed it into a github repo: https://github.com/mlsorensen/perl-Ceph-Rados And fixed the list pools as mentioned. It still needs some work, but github is a better place to collaborate if people are improving it.
Re^2: [Ceph::RADOS] Help Debugging Inline C
by syphilis (Archbishop) on Nov 07, 2013 at 22:17 UTC
    Using Newx (a malloc) will just leak the memory

    How would that happen ? Surely the allocated memory is being freed (Safefree(b)) before the function returns ?

    Cheers,
    Rob
      You are trying to free b which points somewhere into the allocated buffer. I hope you do make use of malloc debuggers :-)

      Also, if rados_pool_list() returned negative, you'd be allocating 4GB chunks something bogus (not sure what casts and/or checks are involved).

      Like I said, the rados code seems to be in flux. Take a look at librados.cc. Looks messy to me.

        You are trying to free b which points somewhere into the allocated buffer

        Duh!! ... of course ... thanks.

        Cheers,
        Rob.