Re: Fonction C: Segmentation fault

From: Samuel ROZE <samuel(dot)roze(at)gmail(dot)com>
To: pgsql-fr-generale(at)postgresql(dot)org
Subject: Re: Fonction C: Segmentation fault
Date: 2009-10-16 13:14:13
Message-ID: 18e9608a0910160614j3cd04eb9n72acfef2ae4106ff@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-fr-generale

Il y a-t-il en ce vendredi quelqu'un de disponible pour jeter un œil
ou deux au code C ?

Merci d'avance !
Samuel.

2009/10/14 Samuel ROZE <samuel(dot)roze(at)gmail(dot)com>:
> Bonjour à tous,
>
> Je souhaites créer une fonction "parse_url" afin de pouvoir récupérer
> l'hostname, le path, le schéma ou autre chose à partir d'une adresse URL
> (text).
>
> Je souhaites donc créer une fonction C. Seulement, j'ai quelques soucis
> pour la création d'une fonction de base. La fonction doit retourner un
> type composite (i.e. une colonne "scheme" et une colonne "host" avec des
> valeurs de base, pour le moment (pour faire ma première fonction C pour
> PostgreSQL). Bref, voilà le code que j'ai.
>
> Fichier: parse_url.c
> ------------------------------------------------------------------------
> /*
> * parse_url.c
> *
> * Created on: 14 oct. 2009
> * Author: Samuel ROZE <samuel(dot)roze(at)gmail(dot)com>
> */
> #include "postgres.h"
> #include <string.h>
> #include "fmgr.h"
> // Tuple building functions and macros
> #include "access/heapam.h"
> #include "funcapi.h"
>
> #include "parse_url.h"
>
> #ifdef PG_MODULE_MAGIC
> PG_MODULE_MAGIC;
> #endif
>
> PG_FUNCTION_INFO_V1(parse_url);
> Datum parse_url (PG_FUNCTION_ARGS)
> {
> // Vars about the params
> text *str = PG_GETARG_TEXT_P(0);
> int32 length = VARSIZE(str);
>
> // Vars about the pg_url instance
> pg_url *ret = palloc(sizeof(pg_url));
>
> // Some vars which will used to create the composite output type
> TupleDesc tupdesc;
> Datum values[8]; // 8 values
> HeapTuple tuple;
> bool *nulls;
> int tuplen;
>
> // Check NULLs values
> if(PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
> PG_RETURN_NULL();
> }
>
> // Parse URL
> ret->scheme = parse_url_alloc("schema", 7);
> ret->host = parse_url_alloc("host", 5);
>
> // Add datas into the values Datum
> values[0] = PointerGetDatum(ret->scheme);
> values[1] = PointerGetDatum(ret->host);
>
> // Convert values into a composite type
> tuplen = tupdesc->natts;
> nulls = palloc(tuplen * sizeof(bool));
>
> // build tuple from datum array
> tuple = heap_form_tuple(tupdesc, values, nulls);
> // Free null values
> pfree(nulls);
>
> // Return the composite type
> PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
> }
>
> char *parse_url_alloc(const char *s, int length)
> {
> char *p;
>
> p = (char *) palloc(length+1);
> if (p != NULL) {
> memcpy(p, s, length);
> p[length] = 0;
> return p;
> }
> }
> ------------------------------------------------------------------------
>
> Fichier: parse_url.h
> ------------------------------------------------------------------------
> /*
> * parse_url.h
> *
> * Created on: 14 oct. 2009
> * Author: Samuel ROZE <samuel(dot)roze(at)gmail(dot)com>
> */
>
> #ifndef PARSE_URL_H
> #define PARSE_URL_H
>
> Datum parse_url (PG_FUNCTION_ARGS);
> char *parse_url_alloc(const char *s, int length);
>
> typedef struct pg_url {
> char *scheme;
> char *user;
> char *pass;
> char *host;
> unsigned short port;
> char *path;
> char *query;
> char *fragment;
> } pg_url;
>
> #endif /* PARSE_URL_H */
> ------------------------------------------------------------------------
>
> Pour le compilation, voilà ce que j'ai fait:
> ------------------------------------------------------------------------
> $ gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Winline
> -Wdeclaration-after-statement -Wendif-labels -fno-strict-aliasing
> -fwrapv -fpic -I. -I/usr/include/postgresql/server
> -I/usr/include/postgresql/internal -D_GNU_SOURCE -c -o parse_url.o
> parse_url.c -I/usr/include/postgresql/
> -I/usr/local/pgsql/include/server/
> ------------------------------------------------------------------------
> $ gcc -shared -o parse_url.so parse_url.o
> ------------------------------------------------------------------------
>
> Ça marche bien. Quelques petites erreurs:
> ------------------------------------------------------------------------
> parse_url.c: In function ‘parse_url’:
> parse_url.c:25: warning: unused variable ‘length’
> parse_url.c: In function ‘parse_url_alloc’:
> parse_url.c:73: warning: control reaches end of non-void function
> parse_url.c: In function ‘parse_url’:
> parse_url.c:51: warning: ‘tupdesc’ may be used uninitialized in this
> function
> ------------------------------------------------------------------------
>
> Pour la création de la fonction dans PostgreSQL:
> ------------------------------------------------------------------------
>
> ------------------------------------------------------------------------
>
> Pour l'éxécution:
> ------------------------------------------------------------------------
> ------------------------------------------------------------------------
>
> L'erreur suivant m'est retounée:
> ------------------------------------------------------------------------
>
>

In response to

Browse pgsql-fr-generale by date

  From Date Subject
Next Message Samuel ROZE 2009-10-17 16:17:42 Re: PDO (PHP) et RAISE NOTICE
Previous Message Samuel ROZE 2009-10-15 17:42:34 Re: Fonction C: Segmentation fault