Re: user defined type

From: Kjetil Haaland <kjetil(dot)haaland(at)student(dot)uib(dot)no>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: user defined type
Date: 2004-11-19 16:07:34
Message-ID: 200411191707.34665.kjetil.haaland@student.uib.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Friday 19 November 2004 16:16, Tom Lane wrote:
> This code is wrong on its face. If your compiler doesn't give a warning
> along the lines of "use of uninitialized value", get a better compiler.
> (Note: when using gcc I think you must specify -O to get this warning.
> I always use at least -O -Wall when developing with gcc.)
>
> > This will give me NOTICE: result=alignres_out.
>
> Pure luck that it doesn't crash instead. Apparently you're picking up a
> value that happened to be left in a register by the function-call
> manager, but on another machine or after any slight mod to fmgr.c that
> register might contain something that's not a pointer to string at all.
>
> regards, tom lane

Ok, the example wasnt complete, i just tried to show what i meant, so i try
again with the hole example. Now i use the \O and it gives no warnings.

Datum alignres_out(PG_FUNCTION_ARGS) {
alignres *align = (alignres *) PG_GETARG_POINTER(0);
char *result = NULL;
int secondStart=align->secondString;
elog(NOTICE, "secondStart=%d", secondStart);
char temp[4+strlen(align->stringBuffer)+4+4];
char tempBuffer[strlen(align->stringBuffer)+1];

char *first = NULL;
elog(NOTICE, "1:first=%s", first);
first = palloc(sizeof(char)*secondStart);
elog(NOTICE, "2:first=%s", first);
elog(NOTICE, "strlen(first)=%d", strlen(first));

snprintf(tempBuffer, sizeof(tempBuffer), "%s", align->stringBuffer);
first = strncpy(first, tempBuffer, secondStart-1);
snprintf(temp, sizeof(temp), "(%d, %s)",
align->value,
first);
result = (char*) palloc(strlen(temp)+1);
strcpy(result, temp);
PG_RETURN_CSTRING(result);
}

On the first elog i get (null) and thats correct. After the allocation i get
alignres_out. When i print out the variable secondString it is 5, but when i
print out the length of the first it is set to 12, the length of
"alignres_out".

Am i still doing something wrong? If so, how am i supposed to do it to get it
correct?

-Kjetil

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2004-11-19 16:20:14 Re: user defined type
Previous Message Tom Lane 2004-11-19 15:16:10 Re: user defined type