Server Programming in C: palloc() and pfree()
I'm having some problems when using "pfree()" on functions in C.
Calling it on "psql" gives the exception below on both versions of function
"insert()" [1,2] if "pfree()" is enabled:
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
conexão com servidor foi perdida
(connection to the server was lost)
The strange is that it doesn't happen with the function "delstr()" [3],
which has "pfree()".
What could am I doing wrong?
// 1)
void insert(char *str, const int start, const char *piece)
{
int lstr = strlen(str);
int lnew = lstr + strlen(piece) + 1;
char* temp = palloc(lnew);
memset((void*) temp, 0, lnew);
/*
FILE *debug;
debug = fopen("/dev/xconsole", "w");
fprintf(debug, "insert('%s', %d, '%s')\n", str, start, piece);
//fprintf(debug, "0) '%s'\n", temp);
*/
if (start <= lstr + 1)
{
strncpy(temp, str, start - 1);
strcat(temp, piece);
char* ptr = str + start - 1;
strcat(temp, ptr);
strcpy(str, temp);
}
// pfree(temp); // <-- here it doesn't work...
/*
fprintf(debug, "-> '%s'\n", str);
fflush(debug);
fclose(debug);
*/
}
// 2)
void insert(char *str, const int start, const char *piece)
{
int i, j;
char* temp = palloc(strlen(str) + strlen(piece) + 1);
if (start - 1 <= strlen(str))
{
for (i = 0; i < start - 1; i++)
temp[i] = str[i];
for (j = i; j < strlen(piece) + i; j++)
temp[j] = piece[j - i];
for (; i < strlen(str); i++, j++)
temp[j] = str[i];
temp[j] = '\0';
strcpy(str, temp);
}
// pfree(temp); // doesn't work...
}
// 3)
void delstr(char *str, const int start, const int size)
{
int i, j;
char* temp = palloc(strlen(str) - size + 1);
for (i = 0; (i < start - 1) && (i < strlen(str)); i++)
temp[i] = str[i];
for (j = start + size - 1; j < strlen(str); i++, j++)
temp[i] = str[j];
temp[i] = '\0';
strcpy(str, temp);
pfree(temp); // <-- here it works!
}
--
Regards,
Rodrigo Hjort
GTI - Projeto PostgreSQL
CELEPAR - Cia de Informática do Paraná
http://www.pr.gov.br
On Thu, Feb 09, 2006 at 04:16:51PM -0200, Rodrigo Hjort wrote:
I'm having some problems when using "pfree()" on functions in C.
Calling it on "psql" gives the exception below on both versions of function
"insert()" [1,2] if "pfree()" is enabled:server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
conexão com servidor foi perdida
(connection to the server was lost)The strange is that it doesn't happen with the function "delstr()" [3],
which has "pfree()".
What could am I doing wrong?
You havn't said how you are calling the functions. For example, did you
declare them as cstring or text? text is not null terminated but a
varlena struct. So if you use text you'll end up with buffer overruns.
Could you provide a complete example with SQL?
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
Show quoted text
Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.
Rodrigo Hjort <rodrigo.hjort@gmail.com> writes:
I'm having some problems when using "pfree()" on functions in C.
I think your pfree is just the bearer of bad news, ie, it's the victim
of a memory clobber that you've already executed. Take another look at
your string manipulation --- that strncpy followed by strcat in
particular looks pretty dangerous, because strncpy doesn't guarantee
a trailing null.
[ looks again... ] Hmm, not to mention that you are overwriting the
input "str", which is bad enough in itself, but you are doing so with
a string longer than the original.
regards, tom lane