pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sascha Schumann <sas@schell.de>
To: Gregory W Burnham <gburnham@sfu.ca>
Cc: S.Ramaswamy <srswamy@giasdl01.vsnl.net.in>
Cc: PETER PAULY <ppauly@usa.net>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Odd characters in inserted data...
Date: Mon, 7 Dec 1998 19:19:10 +0100 (MET)
Message-ID: <Pine.LNX.4.05.9812071915550.6456-100000@guerilla.foo.bar> (raw)
In-Reply-To: <000401be1e14$927876a0$3d063a8e@apemantus.educ.sfu.ca>

On Wed, 2 Dec 1998, Gregory W Burnham wrote:

> >PETER PAULY wrote:
> >
> >> I'm using the "C" interface to write CGI code for a web application.  I
> allow
> >> the user to type data into a particular field, and am storing that data
> into a
> >> field in a postgres database.
> >>
> >> The problem is, I have to filter the data that the user entered to remove
> any
> >> single quotes and other odd characters so that my SQL command doesn't get
> >> messed up.   I'm building the command with printf and passing the
> filtered
> >> data from the user as so:
> >>
> >> update tablename set comment = '%s' where .....
> >>
> >> And %s is substituted in the printf with the user data. If the user typed
> in a
> >> single quote, it would cause havoc with the sql statement.  My question
> is, is
> >
> >you should substitute single quote with two single quotes
> 
> You can also (keeping with 'C' tradition) substitute \' for the single
> quote.

Here is a small C function which escapes a null terminated array of chars
properly. It should be used as

char *dest = alloca(strlen(user_input * 2) + 1);

...

printf("...'%s'...", escape_string(dest, user_input));


char *
escape_string(char *to, char *from)
{
    char *start = to;
    char c;

    for( ; (c = *from); from++) {
        switch(c) {
            case '\\':
            case '\'':
            case '\"':
                *to++ = '\\';
            default:
                *to++ = c;
        }
    }
    *to = '\0';
    return start;
}


                     
          Regards,
						                                
                            Sascha Schumann | 
                                 Consultant | finger sas@schell.de
                                            | for PGP public key




view thread (7+ messages)

Message-ID: <Pine.LNX.4.05.9812071915550.6456-100000@guerilla.foo.bar>
Permalink:  ../Pine.LNX.4.05.9812071915550.6456-100000@guerilla.foo.bar/
Also on:    postgresql.org/message-id/Pine.LNX.4.05.9812071915550.6456-100000@guerilla.foo.bar

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: sas@schell.de, gburnham@sfu.ca, srswamy@giasdl01.vsnl.net.in, ppauly@usa.net
  Subject: Re: [SQL] Odd characters in inserted data...
  In-Reply-To: <Pine.LNX.4.05.9812071915550.6456-100000@guerilla.foo.bar>
  References: <000401be1e14$927876a0$3d063a8e@apemantus.educ.sfu.ca> <Pine.LNX.4.05.9812071915550.6456-100000@guerilla.foo.bar>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox