Skip to content

Commit

Permalink
Fix PG_ErrorInfo and ER_ReturnError when error message is larger than…
Browse files Browse the repository at this point in the history
… 65535 bytes (#70)
  • Loading branch information
r33s3n6 authored Nov 3, 2024
1 parent 7adc6a1 commit e318ab9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
46 changes: 24 additions & 22 deletions environ.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "statement.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "pgapifunc.h"
#ifdef WIN32
#include <winsock2.h>
Expand Down Expand Up @@ -123,32 +124,36 @@ PG_ErrorInfo *
ER_Constructor(SDWORD errnumber, const char *msg)
{
PG_ErrorInfo *error;
ssize_t aladd, errsize;
size_t errsize;
UInt4 aladd;

if (DESC_OK == errnumber)
return NULL;
if (msg)
{
errsize = strlen(msg);
aladd = errsize - sizeof(error->__error_message) + 1;
if (aladd < 0)
if (errsize > UINT32_MAX)
errsize = UINT32_MAX;

if (errsize > sizeof(error->__error_message) - 1)
aladd = errsize - (sizeof(error->__error_message) - 1);
else
aladd = 0;
}
else
{
errsize = -1;
errsize = 0;
aladd = 0;
}
error = (PG_ErrorInfo *) malloc(sizeof(PG_ErrorInfo) + aladd);
if (error)
{
memset(error, 0, sizeof(PG_ErrorInfo));
error->status = errnumber;
error->errorsize = (Int2) errsize;
error->errsize = errsize;
if (errsize > 0)
memcpy(error->__error_message, msg, errsize);
error->__error_message[errsize] = '\0';
error->recsize = -1;
}
return error;
}
Expand All @@ -163,13 +168,13 @@ PG_ErrorInfo *
ER_Dup(const PG_ErrorInfo *self)
{
PG_ErrorInfo *new;
Int4 alsize;
UInt4 alsize;

if (!self)
return NULL;
alsize = sizeof(PG_ErrorInfo);
if (self->errorsize > 0)
alsize += self->errorsize;
if (self->errsize > sizeof(self->__error_message) - 1)
alsize += self->errsize - (sizeof(self->__error_message) - 1);
new = (PG_ErrorInfo *) malloc(alsize);
if (new)
memcpy(new, self, alsize);
Expand All @@ -193,21 +198,20 @@ ER_ReturnError(PG_ErrorInfo *pgerror,
PG_ErrorInfo *error;
BOOL partial_ok = ((flag & PODBC_ALLOW_PARTIAL_EXTRACT) != 0);
const char *msg;
UWORD msglen, wrtlen, pcblen;
UInt4 stapos;
UInt4 stapos, msglen, wrtlen, pcblen;

if (!pgerror)
return SQL_NO_DATA_FOUND;
error = pgerror;
msg = error->__error_message;
MYLOG(0, "entering status = %d, msg = #%s#\n", error->status, msg);
msglen = (SQLSMALLINT) strlen(msg);
msglen = error->errsize;
/*
* Even though an application specifies a larger error message
* buffer, the driver manager changes it silently.
* Therefore we divide the error message into ...
*/
if (error->recsize < 0)
if (error->recsize == 0)
{
if (cbErrorMsgMax > 0)
error->recsize = cbErrorMsgMax - 1; /* apply the first request */
Expand All @@ -216,15 +220,12 @@ ER_ReturnError(PG_ErrorInfo *pgerror,
}
else if (1 == RecNumber && cbErrorMsgMax > 0)
error->recsize = cbErrorMsgMax - 1;
if (RecNumber < 0)
{
if (0 == error->errorpos)
RecNumber = 1;
else
RecNumber = 2 + (error->errorpos - 1) / error->recsize;
}
stapos = (RecNumber - 1) * error->recsize;
if (stapos > msglen)
if (RecNumber > 0)
stapos = (RecNumber - 1) * error->recsize;
else
stapos = error->errpos;

if (stapos >= msglen)
return SQL_NO_DATA_FOUND;
pcblen = wrtlen = msglen - stapos;
if (pcblen > error->recsize)
Expand Down Expand Up @@ -257,6 +258,7 @@ ER_ReturnError(PG_ErrorInfo *pgerror,
if (NULL != szSqlState)
strncpy_null((char *) szSqlState, error->sqlstate, 6);

error->errpos = stapos + wrtlen;
MYLOG(0, " szSqlState = '%s',len=%d, szError='%s'\n", szSqlState, pcblen, szErrorMsg);
if (wrtlen < pcblen)
return SQL_SUCCESS_WITH_INFO;
Expand Down
9 changes: 4 additions & 5 deletions psqlodbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +614,11 @@ typedef struct QueryInfo_
typedef struct
{
UInt4 status;
Int2 errorsize;
Int2 recsize;
Int2 errorpos;
UInt4 errsize;
UInt4 errpos;
UInt2 recsize;
char sqlstate[6];
SQLLEN diag_row_count;
char __error_message[40];
char __error_message[44];
} PG_ErrorInfo;
PG_ErrorInfo *ER_Constructor(SDWORD errornumber, const char *errormsg);
PG_ErrorInfo *ER_Dup(const PG_ErrorInfo *from);
Expand Down
3 changes: 1 addition & 2 deletions statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -1409,9 +1409,8 @@ SC_create_errorinfo(const StatementClass *self, PG_ErrorInfo *pgerror_fail_safe)
memset(pgerror_fail_safe, 0, sizeof(*pgerror_fail_safe));
pgerror = pgerror_fail_safe;
pgerror->status = self->__error_number;
pgerror->errorsize = sizeof(pgerror->__error_message);
STRCPY_FIXED(pgerror->__error_message, ermsg);
pgerror->recsize = -1;
pgerror->errsize = strlen(pgerror->__error_message);
}
else
return NULL;
Expand Down

0 comments on commit e318ab9

Please sign in to comment.