UDF not working correctly

Connectivity
Enthusiast

UDF not working correctly

Hi,

Did write a function ISDATE which will validate yyyy-mm-dd format and send return code of either Correct,Incorrect or Null. When running this function the first time it work correctly, but when rerun or run more than once, then the results became scew, one time the date will be validated correctly sometimes not. There is a posibility that it is a memory leak. Did anyone exprience the same kind of problem and how should i go about solving this

Thanks
Theo
7 REPLIES
Teradata Employee

Re: UDF not working correctly

Hello,

Can you provide the C code?

Regards,

Adeel
Enthusiast

Re: UDF not working correctly



#include
#include
#include
#include

void ISDATEX(char din[10], double *dout)
{
long nM[13];
double ny;
double nm;
int nd;
int i;
int n;
char xy[4];
char xm[4];
char xd[4];

div_t r;

nM[1] = 31;
nM[2] = 28;
nM[3] = 31;
nM[4] = 30;
nM[5] = 31;
nM[6] = 30;
nM[7] = 31;
nM[8] = 31;
nM[9] = 30;
nM[10] = 31;
nM[11] = 30;
nM[12] = 31;

*dout = 1;

if (din[0] == '\0')
{
*dout = 2;
return;
}

n = 0;
for(i=0; i<10; ++i)
{
if (din == ' ')
{
n++;
}
}
if (n == 10)
{
*dout = 2;
return;
}

if (din[4] != '-')
{
*dout = 0;
return;
}
if (din[7] != '-')
{
*dout = 0;
return;
}

xy[0] = '\0';
xy[1] = '\0';
xy[2] = '\0';
xy[3] = '\0';

for(i=0; i<4; ++i)
{
if (!isdigit(din))
{
*dout = 0;
return;
}
else
{
xy = din;
}
}
ny = atof(xy);
if (!(ny > 0 && ny < 3000))
{
*dout = 0;
return;
}
r = div(ny, 4);
if (r.rem == 0)
nM[2] = 29;
else
nM[2] = 28;

xm[0] = '\0';
xm[1] = '\0';

for(i=5; i<7; ++i)
{
if (!isdigit(din))
{
*dout = 0;
return;
}
else
{
xm[i-5] = din;
}
}
nm = atof(xm);
if (!(nm > 0 && nm < 13))
{
*dout = 0;
return;
}

xd[0] = '\0';
xd[1] = '\0';

for(i=8; i<10; ++i)
{
if (!isdigit(din))
{
*dout = 0;
return;
}
else
{
xd[i-8] = din;
}
}
nd = atof(xd);
int mm = (int)nm;
if (!(nd > 0 && nd <= nM[mm]))
{
*dout = 0;
return;
}
*dout = 1;
return;
}
Enthusiast

Re: UDF not working correctly

The includes as follow

stdio.h
string.h
stdlib.h
ctype.h
Teradata Employee

Re: UDF not working correctly

Why you have second input parameter as "Double"? Though you are putting in an integer. Try by changing it to integer.

HTH.

Regards,

Adeel
Enthusiast

Re: UDF not working correctly

Problem happen also when integer
rgs
Enthusiast

Re: UDF not working correctly

You need to look at the Teradata documentation as to how to build a proper UDF prototype. What you have there is not correct. We can’t really provide any help until you let is know what your CREATE function statement looks like. If you don’t specify it correctly all sorts of strange things can happen, including the system crashing on you.

But in looking at the code I see you have the following

xy[0] = '\0';
xy[1] = '\0';
xy[2] = '\0';
xy[3] = '\0';

for(i=0; i<4; ++i)
{
if (!isdigit(din))
{
*dout = 0;
return;
}
else
{
xy = din;
}
}
ny = atof(xy);
if (!(ny > 0 && ny < 3000))
{
*dout = 0;
return;
}

You call atof which expects a string as input. The problem is that the string is not terminated. You need to define xy as:

char xy[5];

and then null the last position that so that the string is terminated.

xy[4] = ‘\0’;

The other areas where you use atof have similar problems.

I think that is the major problem atof is probably picking up garbage and giving a bad convert with a number out of range. It is going to be random depending on what was in memory.

Enthusiast

Re: UDF not working correctly

Thanks for the input, did make use of the termination '\0' character and looks like it solve my problem