You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Simone Caruso <in...@simonecaruso.com> on 2011/01/26 16:28:16 UTC

I don't understand apr_array_push

Hi list,

i can't understand the reason i can't read from an "apr array", my code is like this:
	
	//The 2 arrays, now working on reqc->aliases
	reqc->aliases = (apr_array_header_t *)apr_array_make(r->pool, 5, sizeof(alias_t));
         reqc->redirects = (apr_array_header_t *)apr_array_make(r->pool, 5, sizeof(alias_t));

	while (attributes[i]) {
                         if (strcasecmp (attributes[i], "apacheServerName") == 0) {
                                 reqc->name = apr_pstrdup (r->pool, vals[i]);
                         } else if (strcasecmp (attributes[i], "apacheServerAdmin") == 0) {
                                 reqc->admin = apr_pstrdup (r->pool, vals[i]);
                         } else if (strcasecmp (attributes[i], "apacheDocumentRoot") == 0) {
                                 reqc->docroot = apr_pstrdup (r->pool, vals[i]);
                         } else if (strcasecmp (attributes[i], "apacheScriptAlias") == 0) {
                                 cur = strstr(vals[i], " ");
                                 if(cur - vals[i] > 1 ){
                                         tmp = apr_palloc(r->pool, sizeof(char)*strlen(vals[i]));
                                         strcpy(tmp, vals[i]);
                                         tok = NULL;
                                         alias = apr_array_push(reqc->aliases);//HERE THE PUSH
                                         alias->src = apr_strtok((char *)tmp , " ", &tok);
                                         alias->dst = apr_strtok(NULL, " ", &tok);
                                         alias->iscgi = 1;
                                         isalias = 1;
                                 }else{
                                         ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
                                                 "[mod_vhost_ldap_ng.c]: Wrong apacheScriptAlias paramter: %s", vals[i]);
                                 }
                         } else if (strcasecmp (attributes[i], "apacheAlias") == 0) {
                                 cur = strstr(vals[i], " ");
                                 if(cur - vals[i] > 1 ){
                                         tmp = apr_palloc(r->pool, sizeof(char)*strlen(vals[i]));
                                         strcpy(tmp, vals[i]);
                                         tok = NULL;
                                         alias = apr_array_push(reqc->aliases); //HERE THE PUSH
                                         alias->src = apr_strtok((char *)vals[i] , " ", &tok);
                                         alias->dst = apr_strtok(NULL, " ", &tok);
                                         alias->iscgi = 0;
                                         isalias = 1;


Now i need to read the array (i read mod_alias.c):
                 for(k = 0; k < reqc->aliases->nelts ; k++){
                         alias = (alias_t *)&reqc->aliases->elts[k];
                         isalias = alias_matches(r->uri, alias->src);
                         if(isalias > 0)
                                 break;
                 }


I don't understand why, when k=2, alias structure is filled with wrong addresses:

(gdb)
658                             isalias = alias_matches(r->uri, alias->src);
(gdb)

Program received signal SIGSEGV, Segmentation fault.
0x00007fc350e6784f in alias_matches (uri=0x94f820 "/",
     alias_fakename=0xda000000000094ff <Address 0xda000000000094ff out of bounds>)


I tried a for like this without success (like mod_alias.c):
for(k = 0; k < reqc->aliases->nelts -1 ; ++k){


Any idea?
thanks!


-- 
Simone Caruso
IT Consultant
+39 349 65 90 805
p.iva: 03045250838

Re: I don't understand apr_array_push

Posted by Sorin Manolache <so...@gmail.com>.
> In my opinion the problem is here:
> alias = (alias_t *)&reqc->aliases->elts[k];
>
> Try
>
> alias = ((alias_t *)reqc->aliases->elts)[k];
>

Sorry

alias = &((alias_t *)reqc->aliases->elts)[k];

or equivalently

alias = ((alias_t *)reqc->aliases->elts) + k;

Re: I don't understand apr_array_push

Posted by Sorin Manolache <so...@gmail.com>.
In my opinion the problem is here:
alias = (alias_t *)&reqc->aliases->elts[k];

Try

alias = ((alias_t *)reqc->aliases->elts)[k];

S

On Wed, Jan 26, 2011 at 16:28, Simone Caruso <in...@simonecaruso.com> wrote:
> Hi list,
>
> i can't understand the reason i can't read from an "apr array", my code is
> like this:
>
>        //The 2 arrays, now working on reqc->aliases
>        reqc->aliases = (apr_array_header_t *)apr_array_make(r->pool, 5,
> sizeof(alias_t));
>        reqc->redirects = (apr_array_header_t *)apr_array_make(r->pool, 5,
> sizeof(alias_t));
>
>        while (attributes[i]) {
>                        if (strcasecmp (attributes[i], "apacheServerName") ==
> 0) {
>                                reqc->name = apr_pstrdup (r->pool, vals[i]);
>                        } else if (strcasecmp (attributes[i],
> "apacheServerAdmin") == 0) {
>                                reqc->admin = apr_pstrdup (r->pool, vals[i]);
>                        } else if (strcasecmp (attributes[i],
> "apacheDocumentRoot") == 0) {
>                                reqc->docroot = apr_pstrdup (r->pool,
> vals[i]);
>                        } else if (strcasecmp (attributes[i],
> "apacheScriptAlias") == 0) {
>                                cur = strstr(vals[i], " ");
>                                if(cur - vals[i] > 1 ){
>                                        tmp = apr_palloc(r->pool,
> sizeof(char)*strlen(vals[i]));
>                                        strcpy(tmp, vals[i]);
>                                        tok = NULL;
>                                        alias =
> apr_array_push(reqc->aliases);//HERE THE PUSH
>                                        alias->src = apr_strtok((char *)tmp ,
> " ", &tok);
>                                        alias->dst = apr_strtok(NULL, " ",
> &tok);
>                                        alias->iscgi = 1;
>                                        isalias = 1;
>                                }else{
>                                        ap_log_rerror(APLOG_MARK,
> APLOG_DEBUG|APLOG_NOERRNO, 0, r,
>                                                "[mod_vhost_ldap_ng.c]: Wrong
> apacheScriptAlias paramter: %s", vals[i]);
>                                }
>                        } else if (strcasecmp (attributes[i], "apacheAlias")
> == 0) {
>                                cur = strstr(vals[i], " ");
>                                if(cur - vals[i] > 1 ){
>                                        tmp = apr_palloc(r->pool,
> sizeof(char)*strlen(vals[i]));
>                                        strcpy(tmp, vals[i]);
>                                        tok = NULL;
>                                        alias =
> apr_array_push(reqc->aliases); //HERE THE PUSH
>                                        alias->src = apr_strtok((char
> *)vals[i] , " ", &tok);
>                                        alias->dst = apr_strtok(NULL, " ",
> &tok);
>                                        alias->iscgi = 0;
>                                        isalias = 1;
>
>
> Now i need to read the array (i read mod_alias.c):
>                for(k = 0; k < reqc->aliases->nelts ; k++){
>                        alias = (alias_t *)&reqc->aliases->elts[k];
>                        isalias = alias_matches(r->uri, alias->src);
>                        if(isalias > 0)
>                                break;
>                }
>
>
> I don't understand why, when k=2, alias structure is filled with wrong
> addresses:
>
> (gdb)
> 658                             isalias = alias_matches(r->uri, alias->src);
> (gdb)
>
> Program received signal SIGSEGV, Segmentation fault.
> 0x00007fc350e6784f in alias_matches (uri=0x94f820 "/",
>    alias_fakename=0xda000000000094ff <Address 0xda000000000094ff out of
> bounds>)
>
>
> I tried a for like this without success (like mod_alias.c):
> for(k = 0; k < reqc->aliases->nelts -1 ; ++k){
>
>
> Any idea?
> thanks!
>
>
> --
> Simone Caruso
> IT Consultant
> +39 349 65 90 805
> p.iva: 03045250838
>

Re: I don't understand apr_array_push

Posted by Simone Caruso <in...@simonecaruso.com>.
On 26/01/2011 16:28, Simone Caruso wrote:
> Hi list,
>
> i can't understand the reason i can't read from an "apr array", my code is like this:
>
> //The 2 arrays, now working on reqc->aliases
> reqc->aliases = (apr_array_header_t *)apr_array_make(r->pool, 5, sizeof(alias_t));
> reqc->redirects = (apr_array_header_t *)apr_array_make(r->pool, 5, sizeof(alias_t));
>
> while (attributes[i]) {
> if (strcasecmp (attributes[i], "apacheServerName") == 0) {
> reqc->name = apr_pstrdup (r->pool, vals[i]);
> } else if (strcasecmp (attributes[i], "apacheServerAdmin") == 0) {
> reqc->admin = apr_pstrdup (r->pool, vals[i]);
> } else if (strcasecmp (attributes[i], "apacheDocumentRoot") == 0) {
> reqc->docroot = apr_pstrdup (r->pool, vals[i]);
> } else if (strcasecmp (attributes[i], "apacheScriptAlias") == 0) {
> cur = strstr(vals[i], " ");
> if(cur - vals[i] > 1 ){
> tmp = apr_palloc(r->pool, sizeof(char)*strlen(vals[i]));
> strcpy(tmp, vals[i]);
> tok = NULL;
> alias = apr_array_push(reqc->aliases);//HERE THE PUSH
> alias->src = apr_strtok((char *)tmp , " ", &tok);
> alias->dst = apr_strtok(NULL, " ", &tok);
> alias->iscgi = 1;
> isalias = 1;
> }else{
> ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
> "[mod_vhost_ldap_ng.c]: Wrong apacheScriptAlias paramter: %s", vals[i]);
> }
> } else if (strcasecmp (attributes[i], "apacheAlias") == 0) {
> cur = strstr(vals[i], " ");
> if(cur - vals[i] > 1 ){
> tmp = apr_palloc(r->pool, sizeof(char)*strlen(vals[i]));
> strcpy(tmp, vals[i]);
> tok = NULL;
> alias = apr_array_push(reqc->aliases); //HERE THE PUSH
> alias->src = apr_strtok((char *)vals[i] , " ", &tok);
> alias->dst = apr_strtok(NULL, " ", &tok);
> alias->iscgi = 0;
> isalias = 1;
>
>
> Now i need to read the array (i read mod_alias.c):
> for(k = 0; k < reqc->aliases->nelts ; k++){
> alias = (alias_t *)&reqc->aliases->elts[k];
> isalias = alias_matches(r->uri, alias->src);
> if(isalias > 0)
> break;
> }
>
>
> I don't understand why, when k=2, alias structure is filled with wrong addresses:
>
> (gdb)
> 658 isalias = alias_matches(r->uri, alias->src);
> (gdb)
>
> Program received signal SIGSEGV, Segmentation fault.
> 0x00007fc350e6784f in alias_matches (uri=0x94f820 "/",
> alias_fakename=0xda000000000094ff <Address 0xda000000000094ff out of bounds>)
>
>
> I tried a for like this without success (like mod_alias.c):
> for(k = 0; k < reqc->aliases->nelts -1 ; ++k){
>
>
> Any idea?
> thanks!
>
>

Ehm.. i solved the problem in this way:
alias_t *aliasp = (alias_t *)reqc->aliases->elts;
for(k = 0; k < reqc->aliases->nelts ; k++){
	alias = (alias_t *) &aliasp[k];
	isalias = alias_matches(r->uri, alias->src);
	if(isalias > 0)
		break;

Thanks anyway!

-- 
Simone Caruso
IT Consultant
+39 349 65 90 805
p.iva: 03045250838

Re: I don't understand apr_array_push

Posted by Jim Jagielski <ji...@jaguNET.com>.
On Jan 26, 2011, at 10:28 AM, Simone Caruso wrote:
> 
> Now i need to read the array (i read mod_alias.c):
>                for(k = 0; k < reqc->aliases->nelts ; k++){
>                        alias = (alias_t *)&reqc->aliases->elts[k];
>                        isalias = alias_matches(r->uri, alias->src);
>                        if(isalias > 0)
>                                break;
>                }
> 
> 

Do this:

    alias = (alias_t *)reqc->aliases->elts;
    for (k = 0; k < reqc->aliases->nelts ; k++, alias++){
        isalias = alias_matches(r->uri, alias->src);
        if(isalias > 0)
            break;
    }

> I don't understand why, when k=2, alias structure is filled with wrong addresses:
> 
> (gdb)
> 658                             isalias = alias_matches(r->uri, alias->src);
> (gdb)
> 
> Program received signal SIGSEGV, Segmentation fault.
> 0x00007fc350e6784f in alias_matches (uri=0x94f820 "/",
>    alias_fakename=0xda000000000094ff <Address 0xda000000000094ff out of bounds>)
> 
> 
> I tried a for like this without success (like mod_alias.c):
> for(k = 0; k < reqc->aliases->nelts -1 ; ++k){
> 
> 
> Any idea?
> thanks!
> 
> 
> -- 
> Simone Caruso
> IT Consultant
> +39 349 65 90 805
> p.iva: 03045250838
>