Re: Some encoding trouble via libpq

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Billy Gray" <billy(dot)zophar(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Some encoding trouble via libpq
Date: 2007-03-29 19:46:12
Message-ID: 29814.1175197572@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Billy Gray" <billy(dot)zophar(at)gmail(dot)com> writes:
> char *buffer = (char *) xmalloc (STDIN_BLOCK); //xmalloc is really
> malloc
> int offset = 0;
> int read = 1;
> int size = STDIN_BLOCK;

> while ( (read > 0) && (offset <= STDIN_MAX) )
> {
> syslog (LOG_DEBUG, "Reading a block...");
> read = fread (buffer + offset, 1, STDIN_BLOCK, stdin);
> offset += read;
> if (read == STDIN_BLOCK)
> {
> size += STDIN_BLOCK;
> buffer = xrealloc (buffer, size);
> }
> } // while

This looks to me like it risks telling fread to read more bytes than
will actually fit in the buffer at the moment. Think about what happens
if fread returns only a partial bufferload on any particular call.
I'm guessing you're clobbering memory ...

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2007-03-29 19:48:14 Re: cutting out the middleperl
Previous Message Tom Lane 2007-03-29 19:42:03 Re: Postgres Crash Running PLPGSQL Function on 8.2.3