7502: A system trap was caused by UDF/XSP/UDM ALL_ETL_UDF

Analytics

7502: A system trap was caused by UDF/XSP/UDM ALL_ETL_UDF

I wrote a UDF to read the string and if there are any control characters replace with spaces
when I run the udf one time is working and next time its giving 7502 system trap error and if I run again its working, I am not sure what needs to fix, can anyone help

I am also pasting the code

select myreplace(loss_desc)
from xxx

#define SQL_TEXT Latin_Text
#include "sqltypes_td.h"
#include
#include
#include
#include
#define MAX_STRING_LENGTH 256
#define IsNull -1
#define IsNotNull 0
#define NoSqlError "00000"
#define NUL '\0'

void myreplace( char *inp_str,
char *out_str,
int *inp_strIsNull,
int *out_strIsNull)
{
int i = 0, j = 0;
char *tmp_str ;
*out_str = '\0' ;

memset(out_str, NULL, sizeof(out_str)) ;

tmp_str = malloc(strlen(inp_str)*sizeof(char));
strcpy(tmp_str, inp_str) ;

if (*inp_strIsNull == IsNull)
{
*out_strIsNull = IsNull;
return;
}
*out_strIsNull = IsNotNull;
*out_str = NUL;

if (strlen(inp_str) == 0)
{
*out_str = '\0';
return;
}
while (tmp_str != NULL )
{
if (iscntrl(tmp_str) )
{
tmp_str = ' ';
out_str[j] = toupper(tmp_str);
}
else if (tmp_str == '^' )
{
tmp_str = ' ';
out_str[j] = toupper(tmp_str);
}
else
{
out_str[j] = toupper(tmp_str);
}
++i;
++j;
}
out_str[j] = '\0' ;
free(tmp_str) ;
return;
}