Bug in XCAP_Client

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug in XCAP_Client

Romanov Vladimir

Hi All!

I found bug in \modules\ xcap_client\ xcap_functions.c

Function write_function not work with large content. If you download large content this callback function will call several times. On every call this ficnction create NEW buffer in memory and lost old.

As result you have

1)      Memory leaks

2)      Corrupted content in buffer.

 

This is my variant of this function

 

int str_append(str* buff, char* s, int len)

{

    /* reallocate memory and copy */

    char* newData;

    if (-1==len) {

                len=strlen(s);

    }

    if (0==len) {

                return buff->len;

    }

    newData= (char*)pkg_realloc(buff->s, buff->len + len + 1);

    if(newData== NULL) {

                ERR_MEM(PKG_MEM_STR);

    }

    memcpy(newData+buff->len, s , len);

    buff->s = newData;

    buff->len += len;

    buff->s[buff->len] = 0;

    return buff->len;

error:

    return -1;   

}

 

size_t write_function( void *ptr, size_t size, size_t nmemb, str *buff)

{

    if (str_append(buff,(char*)ptr,(int)(size*nmemb))>=0) {

        return size* nmemb;

    } else {

                LM_ERR("Can't append to string");

                return CURLE_WRITE_ERROR;

    }

}

 

int send_http_get(char* uri, str* buff)

{

    CURLcode ret_code;

    CURL* curl_handle= NULL;

    int http_ret_code=-1;

 

    LM_INFO("Send request %s",uri);

 

    curl_handle = curl_easy_init();

   

    curl_easy_setopt(curl_handle, CURLOPT_URL, uri);

   

    curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);         

 

    curl_easy_setopt(curl_handle, CURLOPT_STDERR, stdout); 

   

    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_function);

   

    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, buff);

 

    /* non-2xx => error */

    curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 0);

 

    ret_code= curl_easy_perform(curl_handle );

   

    if( ret_code!=0) {

                LM_ERR("Error [%i] while performing curl operation", ret_code);

    } else {

                curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_ret_code);

    }

 

    curl_easy_cleanup(curl_handle);

    LM_INFO("send_http_get return %i. Content length=%i", http_ret_code, buff->len);

    return http_ret_code;

}

 

-----------------

Vladimir Romanov

Scartel Star Lab

CTO

+7 (960) 239-0853

 


_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Bug in XCAP_Client

Iñaki Baz Castillo
2009/3/5 Romanov Vladimir <[hidden email]>:
> This is my variant of this function

Hi, could you upload a patch to the SurceForge tracker?

--
Iñaki Baz Castillo
<[hidden email]>

_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Bug in XCAP_Client

Anca Vamanu-2
In reply to this post by Romanov Vladimir
Hi Vladimir,

Thank you for reporting this problem and also giving a solution for it.
I will put your parch in the code.

Thanks and regards,
Anca


Romanov Vladimir wrote:

>
> Hi All!
>
> I found bug in \modules\ xcap_client\ xcap_functions.c
>
> Function write_function not work with large content. If you download
> large content this callback function will call several times. On every
> call this ficnction create NEW buffer in memory and lost old.
>
> As result you have
>
> 1)      Memory leaks
>
> 2)      Corrupted content in buffer.
>
>  
>
> This is my variant of this function
>
>  
>
> int str_append(str* buff, char* s, int len)
>
> {
>
>     /* reallocate memory and copy */
>
>     char* newData;
>
>     if (-1==len) {
>
>                 len=strlen(s);
>
>     }
>
>     if (0==len) {
>
>                 return buff->len;
>
>     }
>
>     newData= (char*)pkg_realloc(buff->s, buff->len + len + 1);
>
>     if(newData== NULL) {
>
>                 ERR_MEM(PKG_MEM_STR);
>
>     }
>
>     memcpy(newData+buff->len, s , len);
>
>     buff->s = newData;
>
>     buff->len += len;
>
>     buff->s[buff->len] = 0;
>
>     return buff->len;
>
> error:
>
>     return -1;  
>
> }
>
>  
>
> size_t write_function( void *ptr, size_t size, size_t nmemb, str *buff)
>
> {
>
>     if (str_append(buff,(char*)ptr,(int)(size*nmemb))>=0) {
>
>         return size* nmemb;
>
>     } else {
>
>                 LM_ERR("Can't append to string");
>
>                 return CURLE_WRITE_ERROR;
>
>     }
>
> }
>
>  
>
> int send_http_get(char* uri, str* buff)
>
> {
>
>     CURLcode ret_code;
>
>     CURL* curl_handle= NULL;
>
>     int http_ret_code=-1;
>
>  
>
>     LM_INFO("Send request %s",uri);
>
>  
>
>     curl_handle = curl_easy_init();
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_URL, uri);
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);        
>
>  
>
>     curl_easy_setopt(curl_handle, CURLOPT_STDERR, stdout);
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_function);
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, buff);
>
>  
>
>     /* non-2xx => error */
>
>     curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 0);
>
>  
>
>     ret_code= curl_easy_perform(curl_handle );
>
>    
>
>     if( ret_code!=0) {
>
>                 LM_ERR("Error [%i] while performing curl operation",
> ret_code);
>
>     } else {
>
>                 curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE,
> &http_ret_code);
>
>     }
>
>  
>
>     curl_easy_cleanup(curl_handle);
>
>     LM_INFO("send_http_get return %i. Content length=%i",
> http_ret_code, buff->len);
>
>     return http_ret_code;
>
> }
>
>  
>
> -----------------
>
> Vladimir Romanov
>
> Scartel Star Lab
>
> CTO
>
> +7 (960) 239-0853
>
>  
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> [hidden email]
> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
>  


_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Bug in XCAP_Client

Anca Vamanu-2
In reply to this post by Romanov Vladimir
Thank you Romanov.
I have now committed the changes in 1.5.x. Could you please update and
test again?

Regards,
Anca

Romanov Vladimir wrote:

>
> Hi All!
>
> I found bug in \modules\ xcap_client\ xcap_functions.c
>
> Function write_function not work with large content. If you download
> large content this callback function will call several times. On every
> call this ficnction create NEW buffer in memory and lost old.
>
> As result you have
>
> 1)      Memory leaks
>
> 2)      Corrupted content in buffer.
>
>  
>
> This is my variant of this function
>
>  
>
> int str_append(str* buff, char* s, int len)
>
> {
>
>     /* reallocate memory and copy */
>
>     char* newData;
>
>     if (-1==len) {
>
>                 len=strlen(s);
>
>     }
>
>     if (0==len) {
>
>                 return buff->len;
>
>     }
>
>     newData= (char*)pkg_realloc(buff->s, buff->len + len + 1);
>
>     if(newData== NULL) {
>
>                 ERR_MEM(PKG_MEM_STR);
>
>     }
>
>     memcpy(newData+buff->len, s , len);
>
>     buff->s = newData;
>
>     buff->len += len;
>
>     buff->s[buff->len] = 0;
>
>     return buff->len;
>
> error:
>
>     return -1;  
>
> }
>
>  
>
> size_t write_function( void *ptr, size_t size, size_t nmemb, str *buff)
>
> {
>
>     if (str_append(buff,(char*)ptr,(int)(size*nmemb))>=0) {
>
>         return size* nmemb;
>
>     } else {
>
>                 LM_ERR("Can't append to string");
>
>                 return CURLE_WRITE_ERROR;
>
>     }
>
> }
>
>  
>
> int send_http_get(char* uri, str* buff)
>
> {
>
>     CURLcode ret_code;
>
>     CURL* curl_handle= NULL;
>
>     int http_ret_code=-1;
>
>  
>
>     LM_INFO("Send request %s",uri);
>
>  
>
>     curl_handle = curl_easy_init();
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_URL, uri);
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);        
>
>  
>
>     curl_easy_setopt(curl_handle, CURLOPT_STDERR, stdout);
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_function);
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, buff);
>
>  
>
>     /* non-2xx => error */
>
>     curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 0);
>
>  
>
>     ret_code= curl_easy_perform(curl_handle );
>
>    
>
>     if( ret_code!=0) {
>
>                 LM_ERR("Error [%i] while performing curl operation",
> ret_code);
>
>     } else {
>
>                 curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE,
> &http_ret_code);
>
>     }
>
>  
>
>     curl_easy_cleanup(curl_handle);
>
>     LM_INFO("send_http_get return %i. Content length=%i",
> http_ret_code, buff->len);
>
>     return http_ret_code;
>
> }
>
>  
>
> -----------------
>
> Vladimir Romanov
>
> Scartel Star Lab
>
> CTO
>
> +7 (960) 239-0853
>
>  
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> [hidden email]
> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
>  


_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Bug in XCAP_Client

Romanov Vladimir
Hi!
I don't use Xcap now. I just want write module to do SSO authentication (using OpenSSO server), and I use XCAP_Client module for starting point to send HTTP request.

-----------------
Vladimir Romanov
Scartel Star Lab
CTO
+7 (960) 239-0853


-----Original Message-----
From: Anca Vamanu [mailto:[hidden email]]
Sent: Thursday, March 05, 2009 8:55 PM
To: Romanov Vladimir
Cc: [hidden email]
Subject: Re: [OpenSIPS-Users] Bug in XCAP_Client

Thank you Romanov.
I have now committed the changes in 1.5.x. Could you please update and
test again?

Regards,
Anca

Romanov Vladimir wrote:

>
> Hi All!
>
> I found bug in \modules\ xcap_client\ xcap_functions.c
>
> Function write_function not work with large content. If you download
> large content this callback function will call several times. On every
> call this ficnction create NEW buffer in memory and lost old.
>
> As result you have
>
> 1)      Memory leaks
>
> 2)      Corrupted content in buffer.
>
>  
>
> This is my variant of this function
>
>  
>
> int str_append(str* buff, char* s, int len)
>
> {
>
>     /* reallocate memory and copy */
>
>     char* newData;
>
>     if (-1==len) {
>
>                 len=strlen(s);
>
>     }
>
>     if (0==len) {
>
>                 return buff->len;
>
>     }
>
>     newData= (char*)pkg_realloc(buff->s, buff->len + len + 1);
>
>     if(newData== NULL) {
>
>                 ERR_MEM(PKG_MEM_STR);
>
>     }
>
>     memcpy(newData+buff->len, s , len);
>
>     buff->s = newData;
>
>     buff->len += len;
>
>     buff->s[buff->len] = 0;
>
>     return buff->len;
>
> error:
>
>     return -1;  
>
> }
>
>  
>
> size_t write_function( void *ptr, size_t size, size_t nmemb, str *buff)
>
> {
>
>     if (str_append(buff,(char*)ptr,(int)(size*nmemb))>=0) {
>
>         return size* nmemb;
>
>     } else {
>
>                 LM_ERR("Can't append to string");
>
>                 return CURLE_WRITE_ERROR;
>
>     }
>
> }
>
>  
>
> int send_http_get(char* uri, str* buff)
>
> {
>
>     CURLcode ret_code;
>
>     CURL* curl_handle= NULL;
>
>     int http_ret_code=-1;
>
>  
>
>     LM_INFO("Send request %s",uri);
>
>  
>
>     curl_handle = curl_easy_init();
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_URL, uri);
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);        
>
>  
>
>     curl_easy_setopt(curl_handle, CURLOPT_STDERR, stdout);
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_function);
>
>    
>
>     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, buff);
>
>  
>
>     /* non-2xx => error */
>
>     curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 0);
>
>  
>
>     ret_code= curl_easy_perform(curl_handle );
>
>    
>
>     if( ret_code!=0) {
>
>                 LM_ERR("Error [%i] while performing curl operation",
> ret_code);
>
>     } else {
>
>                 curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE,
> &http_ret_code);
>
>     }
>
>  
>
>     curl_easy_cleanup(curl_handle);
>
>     LM_INFO("send_http_get return %i. Content length=%i",
> http_ret_code, buff->len);
>
>     return http_ret_code;
>
> }
>
>  
>
> -----------------
>
> Vladimir Romanov
>
> Scartel Star Lab
>
> CTO
>
> +7 (960) 239-0853
>
>  
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> [hidden email]
> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
>  


_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users