---- Daniel Gruno <rum...@cord.dk> wrote: 
> On 06/20/2012 06:17 PM, oh...@cox.net wrote:
> > 
> > Hi Ben and Daniel,
> > 
> > I must be doing wrong.  After I added code to strip the begin and end 
> > lines, I get a segfault when I test.
> > 
> > I notice that the apr_cpystrn() doesn't include a pool parameter, so I 
> > guess it's just moving the char strings in memory that already exists in 
> > the pool?
> > 
> > Here's the code I have thus far:
> > 
> >     const char * ssl_client_cert;
> >     const char * ssl_client_cert_after_remove_begin;
> >     const char * ssl_client_cert_after_remove_end;
> >     int end_of_cert;
> >     
> >     printf("SSL_CLIENT_CERT=[%s]\n", apr_table_get(r->subprocess_env, 
> > "SSL_CLIENT_CERT"));
> >     ssl_client_cert = apr_table_get(r->subprocess_env, "SSL_CLIENT_CERT");
> > 
> >     printf("printf ssl_client_cert=[%s]\n", ssl_client_cert);
> >     printf("ssl_client_cert + 27=[%s]\n", ssl_client_cert+27);
> > 
> > // SEGFAULT after here...
> > 
> >     end_of_cert = strlen(ssl_client_cert);
> >     apr_cpystrn(ssl_client_cert_after_remove_begin, ssl_client_cert+27, 
> > end_of_cert-27  );
> >     end_of_cert = strlen(ssl_client_cert);
> > 
> >     printf("printf ssl_client_cert_after_remove_begin=[%d]-[%s]\n", 
> > end_of_cert, ssl_client_cert_after_remove_begin);
> >     end_of_cert = strlen(ssl_client_cert_after_remove_begin);
> >     apr_cpystrn(ssl_client_cert_after_remove_end, 
> > ssl_client_cert_after_remove_begin+21, end_of_cert-21);
> > 
> >     printf("printf ssl_client_cert_after_remove_end=[%d]-[%s]\n", 
> > end_of_cert, ssl_client_cert_after_remove_end);
> > 
> > Thanks,
> > Jim
> > 
> > 
> apr_cpystrn expects the destination to be already allocated space of the
> correct size.
> 
> You need to either allocate the space for your char* yourself via
> apr_palloc or apr_pcalloc, or use apr_pstrndup instead, if you expect
> httpd to handle the memory management.
> 
> With regards,
> Daniel.


Hi,

I'll try the palloc, but yourself and I think Ben mentioned apr_pstrndup(), and 
I don't quite understand that.  It looks like pstrndup() just creates a new 
string in the pool that's the same size as the original string, but I don't 
quite understand why having that duplicated char string in the pool helps?  

Or, are you all suggesting using apr_pstrndup() to create a copy of string I 
want to manipulate, and then use something like:

 apr_cpystrn(newly_allocated_string, old_string+27, old_string_lgh-27)

Thanks again!

Jim

Reply via email to