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 Micah Yoder <mi...@yoderdev.com> on 2009/12/01 09:26:19 UTC

Segfault doing SQL select

Hello,

I'm new to this. Just read most of Nick Kew's book and am trying to 
write a module that is a thin-as-possible layer between an AJAX type 
rich client web app and an SQL database. I then hope to build a small 
CMS on top of that.

Currently I'm running into a road block with a segfault every time I run 
the query. I'm using Postgres 8.4 from the Ubuntu repos and a 
custom-compiled Apache 2.2.14 with the Worker MPM.  Here is the relevant 
code:

void sql_template(ap_dbd_t *con, request_rec *r, apr_hash_t *formdata) {
  apr_dbd_results_t *res;
  apr_dbd_row_t *row;
  int rv;
 
  rv = apr_dbd_select(con->driver, r->pool, con->handle, &res, "select * 
from topics;", 0);
  if (!rv)
    ap_rputs("Failed to run query.\n", r);
  while (apr_dbd_get_row(con->driver, r->pool, res, &row, 0) != -1) {
    ap_rputs("*** Row data *** ", r);
    ap_rputs(apr_dbd_get_entry(con->driver, row, 0), r);
    ap_rputs(" ... ", r);
    ap_rputs(apr_dbd_get_entry(con->driver, row, 1), r);
  }
}

This is being called from the main handler function.

  apr_hash_t *formdata;
  ap_dbd_t *con;

[...]

  if (r->method_number == M_GET)
    formdata = parse_form_from_string(r, r->args);

[...]

  con = ap_dbd_acquire(r);
  if (!con) return HTTP_INTERNAL_SERVER_ERROR;
 
  sql_template(con, r, formdata);

Kind of ugly but I'm just trying to get the concepts to work.

I've run it through GDB and sometimes the segfault is on apr_dbd_select 
line and sometimes it is on the apr_dbd_get_row line. The arguments 
passed into my sql_template function seem to be valid.

Am I doing anything obviously wrong? Or can someone point to a simple 
handler module that does a SELECT? (mod_auth_dbd appears to use a 
different method and introduces other complexities.)

Thanks,
Micah



Re: Segfault doing SQL select

Posted by Micah Yoder <mi...@yoderdev.com>.
Sorin Manolache wrote:
>> void sql_template(ap_dbd_t *con, request_rec *r, apr_hash_t *formdata) {
>>  apr_dbd_results_t *res;
>>     
>
>
> Set res to NULL before passing it.
>
>   
>>  apr_dbd_row_t *row;
>>     
>
>
> Set row to NULL before passing it.
>
>   
that was it. Thanks so much!

Guess I thought that the functions would have ignored and overridden any 
nonsensical values. Oh well.

And thank you too Jerome, I'm sure I will be studying that code.


Re: Segfault doing SQL select

Posted by Sorin Manolache <so...@gmail.com>.
On Tue, Dec 1, 2009 at 09:26, Micah Yoder <mi...@yoderdev.com> wrote:
> Hello,
>
> I'm new to this. Just read most of Nick Kew's book and am trying to write a
> module that is a thin-as-possible layer between an AJAX type rich client web
> app and an SQL database. I then hope to build a small CMS on top of that.
>
> Currently I'm running into a road block with a segfault every time I run the
> query. I'm using Postgres 8.4 from the Ubuntu repos and a custom-compiled
> Apache 2.2.14 with the Worker MPM.  Here is the relevant code:
>
> void sql_template(ap_dbd_t *con, request_rec *r, apr_hash_t *formdata) {
>  apr_dbd_results_t *res;


Set res to NULL before passing it.

>  apr_dbd_row_t *row;


Set row to NULL before passing it.

>  int rv;
>
>  rv = apr_dbd_select(con->driver, r->pool, con->handle, &res, "select * from
> topics;", 0);
>  if (!rv)
>   ap_rputs("Failed to run query.\n", r);
>  while (apr_dbd_get_row(con->driver, r->pool, res, &row, 0) != -1) {
>   ap_rputs("*** Row data *** ", r);
>   ap_rputs(apr_dbd_get_entry(con->driver, row, 0), r);
>   ap_rputs(" ... ", r);
>   ap_rputs(apr_dbd_get_entry(con->driver, row, 1), r);
>  }
> }
>
> This is being called from the main handler function.
>
>  apr_hash_t *formdata;
>  ap_dbd_t *con;
>
> [...]
>
>  if (r->method_number == M_GET)
>   formdata = parse_form_from_string(r, r->args);
>
> [...]
>
>  con = ap_dbd_acquire(r);
>  if (!con) return HTTP_INTERNAL_SERVER_ERROR;
>
>  sql_template(con, r, formdata);
>
> Kind of ugly but I'm just trying to get the concepts to work.
>
> I've run it through GDB and sometimes the segfault is on apr_dbd_select line
> and sometimes it is on the apr_dbd_get_row line. The arguments passed into
> my sql_template function seem to be valid.
>
> Am I doing anything obviously wrong? Or can someone point to a simple
> handler module that does a SELECT? (mod_auth_dbd appears to use a different
> method and introduces other complexities.)
>
> Thanks,
> Micah
>
>
>



-- 
A: Because it reverses the logical flow of conversation.
Q: Why is top-posting frowned upon?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

Re: Segfault doing SQL select

Posted by Jerome Renard <je...@gmail.com>.
Hello,

On Tue, Dec 1, 2009 at 9:26 AM, Micah Yoder <mi...@yoderdev.com> wrote:
[...]
> Am I doing anything obviously wrong? Or can someone point to a simple
> handler module that does a SELECT? (mod_auth_dbd appears to use a different
> method and introduces other complexities.)

Maybe the piece of code available here will help you :
- http://code.google.com/p/modurlalias/source/browse/src/mod_url_alias.c#454

Best Regards

--
Jérôme :)