Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can return value be modified?
    primarykey
    data
    text
    <p>I have a routine calling <code>gSoap</code> API function <code>soap_malloc</code>. But the program gives me a <code>segmentation fault</code> whenever I try to access the memory allocated by <code>soap_malloc</code>. When I use <code>gdb</code> to debug it. I find that inside <code>soap_malloc</code>, return value stored in register <code>%rax</code> is <code>0x7fffec0018f0</code>. But on return, <code>%rax</code> changed to <code>0xffffffffec0018f0</code>. Only the lower 32-bit was retained, the higher 32-bit all changed to <code>1</code>. And that lead to accessing an address which is quite high, therefore caused the routine stopped. Thanks for you all to give me any ideas on how can this be happening. I'm running my multi-thread program in <code>Ubuntu12.04 x86-64</code>.</p> <p>This is how I call it: </p> <pre><code>void *temp = soap_malloc(soap, 96); </code></pre> <p>And this is the <code>soap_malloc</code> implementation(only that <code>else</code> part is executed, and macro <code>SOAP_MALLOC</code> is just passing the second argument to <code>malloc</code>, <code>SOAP_CANARY</code> is constant <code>0xC0DE</code>):</p> <pre><code>#ifndef SOAP_MALLOC /* use libc malloc */ # define SOAP_MALLOC(soap, size) malloc(size) #endif #ifndef SOAP_CANARY # define SOAP_CANARY (0xC0DE) #endif void* soap_malloc(struct soap *soap, size_t n) { register char *p; if (!n) return (void*)SOAP_NON_NULL; if (!soap) return SOAP_MALLOC(soap, n); if (soap-&gt;fmalloc) p = (char*)soap-&gt;fmalloc(soap, n); else { n += sizeof(short); n += (-(long)n) &amp; (sizeof(void*)-1); /* align at 4-, 8- or 16-byte boundary */ if (!(p = (char*)SOAP_MALLOC(soap, n + sizeof(void*) + sizeof(size_t)))) { soap-&gt;error = SOAP_EOM; return NULL; } /* set the canary to detect corruption */ *(unsigned short*)(p + n - sizeof(unsigned short)) = (unsigned short)SOAP_CANARY; /* keep chain of alloced cells for destruction */ *(void**)(p + n) = soap-&gt;alist; *(size_t*)(p + n + sizeof(void*)) = n; soap-&gt;alist = p + n; } soap-&gt;alloced = 1; return p; } </code></pre> <p>This is the definition of <code>SOAP_NON_NULL</code>:</p> <pre><code>static const char soap_padding[4] = "\0\0\0"; #define SOAP_NON_NULL (soap_padding) </code></pre> <p><strong>Updates(<em>2013-03-12</em>)</strong><br> I explicitly declared <code>soap_malloc</code> as returning <code>void *</code> and the problem solved. Previously, the returned value is truncated to <code>int</code> and the sign bit <code>1</code> was extended when assigning the result to <code>void *temp</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload