You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Graf László <gr...@datatrans.hu> on 2013/05/27 22:21:53 UTC
How to convert char[] to char*?
Hi,
I have a handler module which reads the HTTP response from a remote web
server.
In this module I have a
char[] buf
and it contains the bytes red using apr_socket_recv. With
ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, r->pool,
apr_psprintf(r->pool, "<%s>", buf));
I can see the buf's content in the error_log but the
ap_rputs(apr_psprintf(r->pool, "%s", (char *)buf), r);
fails. How can I convert this array to a char* to be able to put the
HTTP response in the handler's request using ap_rputs?
Best regards,
grafl
RE: How to convert char[] to char*?
Posted by "Bennett, Tony" <Be...@con-way.com>.
Just a wild guess, but you may be accessing beyond the end of the buffer...
You do: apr_status_t rv = apr_socket_recv(sock, buf, &len);
which requests "len" number of bytes, being placed into "buf".
What happens if you get "len" then try to format it using
apr_psprintf(r->pool, "---%s---", bufp)
apr_psprintf() assumes bufp is a string... that is a NULL terminated string,
but you did not guarantee that "len" number of bytes in "buf"
has a NULL following it.
-----Original Message-----
From: Graf László [mailto:grafl@datatrans.hu]
Sent: Wednesday, May 29, 2013 3:06 PM
To: Ben Reser
Cc: dev
Subject: Re: How to convert char[] to char*?
Hi Ben,
this is my code snippet and if I enable the commented line then I get an
Apache error
AH00052: child pid 24982 exit signal Segmentation fault (11)
The snippet:
static apr_status_t do_client_task(apr_socket_t *sock, const char
*filepath, request_rec *r)
{
apr_status_t rv;
const char *req_hdr = apr_pstrcat(r->pool, "GET /", filepath, "
HTTP/1.0" CRLF_STR CRLF_STR, NULL);
apr_size_t len = strlen(req_hdr);
rv = apr_socket_send(sock, req_hdr, &len);
if (rv != APR_SUCCESS) {
return rv;
}
{
char buf[BUFSIZE];
apr_size_t len = sizeof(buf);
char *bufp = 0;
while (1) {
apr_status_t rv = apr_socket_recv(sock, buf, &len);
if (rv == APR_EOF || len == 0) {
break;
}
bufp = apr_palloc(r->pool, len);
bufp = buf;
ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, r->pool,
apr_psprintf(r->pool, "---%s---", bufp));
// ap_rputs(apr_psprintf(r->pool, "---%s---<br/>\n", bufp), r);
}
}
return rv;
}
A hint would help me a lot. Thank you.
Best regards,
grafl
On 2013-05-27 23:25, Ben Reser wrote:
> On Mon, May 27, 2013 at 1:21 PM, Graf László<gr...@datatrans.hu> wrote:
>> I have a handler module which reads the HTTP response from a remote web
>> server.
>> In this module I have a
>>
>> char[] buf
>>
>> and it contains the bytes red using apr_socket_recv. With
>>
>> ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, r->pool, apr_psprintf(r->pool,
>> "<%s>", buf));
>>
>> I can see the buf's content in the error_log but the
>>
>> ap_rputs(apr_psprintf(r->pool, "%s", (char *)buf), r);
>>
>> fails. How can I convert this array to a char* to be able to put the HTTP
>> response in the handler's request using ap_rputs?
> You haven't given enough info to answer your question. However, this
> is really a question of how to write C so I'd point you to references
> on C.
>
> See this question on Stack Overflow:
> http://stackoverflow.com/questions/1790704/difference-between-square-brackets-and-asterisk
>
> (that question is about C++ but as far as I know there's no difference
> between the two in this case)
>
Re: How to convert char[] to char*?
Posted by Bojan Smojver <bo...@rexursive.com>.
On Thu, 2013-05-30 at 00:06 +0200, Graf László wrote:
> bufp = apr_palloc(r->pool, len);
> bufp = buf;
What is the point of the above code? First some memory is allocated for
bufp, then it is ignored, as bufp actually becomes a pointer to the buf
array...
As for the segfault, run you code through gdb (or some other debugger)
and step through it while examining the contents of variables. That
should give you some clues.
--
Bojan
Re: How to convert char[] to char*?
Posted by Graf László <gr...@datatrans.hu>.
Hi Ben,
this is my code snippet and if I enable the commented line then I get an
Apache error
AH00052: child pid 24982 exit signal Segmentation fault (11)
The snippet:
static apr_status_t do_client_task(apr_socket_t *sock, const char
*filepath, request_rec *r)
{
apr_status_t rv;
const char *req_hdr = apr_pstrcat(r->pool, "GET /", filepath, "
HTTP/1.0" CRLF_STR CRLF_STR, NULL);
apr_size_t len = strlen(req_hdr);
rv = apr_socket_send(sock, req_hdr, &len);
if (rv != APR_SUCCESS) {
return rv;
}
{
char buf[BUFSIZE];
apr_size_t len = sizeof(buf);
char *bufp = 0;
while (1) {
apr_status_t rv = apr_socket_recv(sock, buf, &len);
if (rv == APR_EOF || len == 0) {
break;
}
bufp = apr_palloc(r->pool, len);
bufp = buf;
ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, r->pool,
apr_psprintf(r->pool, "---%s---", bufp));
// ap_rputs(apr_psprintf(r->pool, "---%s---<br/>\n", bufp), r);
}
}
return rv;
}
A hint would help me a lot. Thank you.
Best regards,
grafl
On 2013-05-27 23:25, Ben Reser wrote:
> On Mon, May 27, 2013 at 1:21 PM, Graf László<gr...@datatrans.hu> wrote:
>> I have a handler module which reads the HTTP response from a remote web
>> server.
>> In this module I have a
>>
>> char[] buf
>>
>> and it contains the bytes red using apr_socket_recv. With
>>
>> ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, r->pool, apr_psprintf(r->pool,
>> "<%s>", buf));
>>
>> I can see the buf's content in the error_log but the
>>
>> ap_rputs(apr_psprintf(r->pool, "%s", (char *)buf), r);
>>
>> fails. How can I convert this array to a char* to be able to put the HTTP
>> response in the handler's request using ap_rputs?
> You haven't given enough info to answer your question. However, this
> is really a question of how to write C so I'd point you to references
> on C.
>
> See this question on Stack Overflow:
> http://stackoverflow.com/questions/1790704/difference-between-square-brackets-and-asterisk
>
> (that question is about C++ but as far as I know there's no difference
> between the two in this case)
>
Re: How to convert char[] to char*?
Posted by Joshua Marantz <jm...@google.com>.
Two questions:
1. Can you paste the code where you call apr_socket_recv including
(especially) the declaration of buf? How big is the array at its
allocation point?
2. How are you transmitting the length of the string from apr_socket_recv
to ap_rputs?
3. What is the exact nature of the failure you saw?
Make that three questions!
On Mon, May 27, 2013 at 5:25 PM, Ben Reser <be...@reser.org> wrote:
> On Mon, May 27, 2013 at 1:21 PM, Graf László <gr...@datatrans.hu> wrote:
> > I have a handler module which reads the HTTP response from a remote web
> > server.
> > In this module I have a
> >
> > char[] buf
> >
> > and it contains the bytes red using apr_socket_recv. With
> >
> > ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, r->pool, apr_psprintf(r->pool,
> > "<%s>", buf));
> >
> > I can see the buf's content in the error_log but the
> >
> > ap_rputs(apr_psprintf(r->pool, "%s", (char *)buf), r);
> >
> > fails. How can I convert this array to a char* to be able to put the HTTP
> > response in the handler's request using ap_rputs?
>
> You haven't given enough info to answer your question. However, this
> is really a question of how to write C so I'd point you to references
> on C.
>
> See this question on Stack Overflow:
>
> http://stackoverflow.com/questions/1790704/difference-between-square-brackets-and-asterisk
>
> (that question is about C++ but as far as I know there's no difference
> between the two in this case)
>
Re: How to convert char[] to char*?
Posted by Ben Reser <be...@reser.org>.
On Mon, May 27, 2013 at 1:21 PM, Graf László <gr...@datatrans.hu> wrote:
> I have a handler module which reads the HTTP response from a remote web
> server.
> In this module I have a
>
> char[] buf
>
> and it contains the bytes red using apr_socket_recv. With
>
> ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, r->pool, apr_psprintf(r->pool,
> "<%s>", buf));
>
> I can see the buf's content in the error_log but the
>
> ap_rputs(apr_psprintf(r->pool, "%s", (char *)buf), r);
>
> fails. How can I convert this array to a char* to be able to put the HTTP
> response in the handler's request using ap_rputs?
You haven't given enough info to answer your question. However, this
is really a question of how to write C so I'd point you to references
on C.
See this question on Stack Overflow:
http://stackoverflow.com/questions/1790704/difference-between-square-brackets-and-asterisk
(that question is about C++ but as far as I know there's no difference
between the two in this case)