Commit cba46c99 authored by Olivier Bertrand's avatar Olivier Bertrand

- Fix jfile_convert crash on error. modified: jsonudf.cpp (plus BSON UDF's)

parent a3542482
...@@ -808,17 +808,17 @@ void* BJSON::BsonSubAlloc(size_t size) ...@@ -808,17 +808,17 @@ void* BJSON::BsonSubAlloc(size_t size)
/*********************************************************************************/ /*********************************************************************************/
/* Program for SubSet re-initialization of the memory pool. */ /* Program for SubSet re-initialization of the memory pool. */
/*********************************************************************************/ /*********************************************************************************/
OFFSET BJSON::DupStr(PSZ str) PSZ BJSON::NewStr(PSZ str)
{ {
if (str) { if (str) {
PSZ sm = (PSZ)BsonSubAlloc(strlen(str) + 1); PSZ sm = (PSZ)BsonSubAlloc(strlen(str) + 1);
strcpy(sm, str); strcpy(sm, str);
return MOF(sm); return sm;
} else } else
return NULL; return NULL;
} // end of DupStr } // end of NewStr
/*********************************************************************************/ /*********************************************************************************/
/* Program for SubSet re-initialization of the memory pool. */ /* Program for SubSet re-initialization of the memory pool. */
...@@ -940,7 +940,7 @@ PBVAL BJSON::GetObjectValList(PBVAL bop) ...@@ -940,7 +940,7 @@ PBVAL BJSON::GetObjectValList(PBVAL bop)
PBVAL arp = NewVal(TYPE_JAR); PBVAL arp = NewVal(TYPE_JAR);
for (PBPR brp = GetObject(bop); brp; brp = GetNext(brp)) for (PBPR brp = GetObject(bop); brp; brp = GetNext(brp))
AddArrayValue(arp, GetVlp(brp)); AddArrayValue(arp, DupVal(GetVlp(brp)));
return arp; return arp;
} // end of GetObjectValList } // end of GetObjectValList
...@@ -1135,24 +1135,28 @@ PBVAL BJSON::GetArrayValue(PBVAL bap, int n) ...@@ -1135,24 +1135,28 @@ PBVAL BJSON::GetArrayValue(PBVAL bap, int n)
/***********************************************************************/ /***********************************************************************/
/* Add a Value to the Array Value list. */ /* Add a Value to the Array Value list. */
/***********************************************************************/ /***********************************************************************/
void BJSON::AddArrayValue(PBVAL bap, OFFSET nvp, int* x) void BJSON::AddArrayValue(PBVAL bap, OFFSET nbv, int* x)
{ {
CheckType(bap, TYPE_JAR); CheckType(bap, TYPE_JAR);
if (!nvp) int i = 0;
nvp = MOF(NewVal()); PBVAL bvp, lbp = NULL;
if (bap->To_Val) { if (!nbv)
int i = 0, n = (x) ? *x : INT_MAX32; nbv = MOF(NewVal());
for (PBVAL bvp = GetArray(bap); bvp; bvp = GetNext(bvp), i++) for (bvp = GetArray(bap); bvp; bvp = GetNext(bvp), i++)
if (!bvp->Next || (x && i == n)) { if (x && i == *x)
MVP(nvp)->Next = bvp->Next; break;
bvp->Next = nvp; else
break; lbp = bvp;
} // endif Next
} else if (lbp) {
bap->To_Val = nvp; MVP(nbv)->Next = lbp->Next;
lbp->Next = nbv;
} else {
MVP(nbv)->Next = bap->To_Val;
bap->To_Val = nbv;
} // endif lbp
bap->Nd++; bap->Nd++;
} // end of AddArrayValue } // end of AddArrayValue
......
...@@ -93,7 +93,8 @@ class BJSON : public BLOCK { ...@@ -93,7 +93,8 @@ class BJSON : public BLOCK {
PBVAL SubAllocStr(PSZ str, short nd = 0) PBVAL SubAllocStr(PSZ str, short nd = 0)
{return SubAllocStr(DupStr(str), nd);} {return SubAllocStr(DupStr(str), nd);}
PBVAL DupVal(PBVAL bvp); PBVAL DupVal(PBVAL bvp);
OFFSET DupStr(PSZ str); OFFSET DupStr(PSZ str) { return MOF(NewStr(str)); }
PSZ NewStr(PSZ str);
// Array functions // Array functions
inline PBVAL GetArray(PBVAL vlp) {return MVP(vlp->To_Val);} inline PBVAL GetArray(PBVAL vlp) {return MVP(vlp->To_Val);}
...@@ -150,7 +151,8 @@ class BJSON : public BLOCK { ...@@ -150,7 +151,8 @@ class BJSON : public BLOCK {
void SetBool(PBVAL vlp, bool b); void SetBool(PBVAL vlp, bool b);
void Clear(PBVAL vlp) { vlp->N = 0; vlp->Nd = 0; vlp->Next = 0; } void Clear(PBVAL vlp) { vlp->N = 0; vlp->Nd = 0; vlp->Next = 0; }
bool IsValueNull(PBVAL vlp); bool IsValueNull(PBVAL vlp);
bool IsJson(PBVAL vlp) {return (vlp->Type == TYPE_JAR || vlp->Type == TYPE_JOB);} bool IsJson(PBVAL vlp)
{return vlp ? vlp->Type == TYPE_JAR || vlp->Type == TYPE_JOB : false;}
// Members // Members
PGLOBAL G; PGLOBAL G;
......
This diff is collapsed.
...@@ -9,7 +9,77 @@ ...@@ -9,7 +9,77 @@
#include "jsonudf.h" #include "jsonudf.h"
#include "bson.h" #include "bson.h"
#if 0
#define UDF_EXEC_ARGS \
UDF_INIT*, UDF_ARGS*, char*, unsigned long*, char*, char*
// BSON size should be equal on Linux and Windows
#define BMX 255
typedef struct BSON* PBSON;
/***********************************************************************/
/* Structure used to return binary json to Json UDF functions. */
/***********************************************************************/
struct BSON {
char Msg[BMX + 1];
char *Filename;
PGLOBAL G;
int Pretty;
ulong Reslen;
my_bool Changed;
PJSON Top;
PJSON Jsp;
PBSON Bsp;
}; // end of struct BSON
PBSON JbinAlloc(PGLOBAL g, UDF_ARGS* args, ulong len, PJSON jsp);
/*********************************************************************************/
/* The JSON tree node. Can be an Object or an Array. */
/*********************************************************************************/
typedef struct _jnode {
PSZ Key; // The key used for object
OPVAL Op; // Operator used for this node
PVAL CncVal; // To cont value used for OP_CNC
PVAL Valp; // The internal array VALUE
int Rank; // The rank in array
int Rx; // Read row number
int Nx; // Next to read row number
} JNODE, *PJNODE;
/*********************************************************************************/
/* The JSON utility functions. */
/*********************************************************************************/
bool IsNum(PSZ s);
char *NextChr(PSZ s, char sep);
char *GetJsonNull(void);
uint GetJsonGrpSize(void);
my_bool JsonSubSet(PGLOBAL g, my_bool b = false);
my_bool CalcLen(UDF_ARGS* args, my_bool obj, unsigned long& reslen,
unsigned long& memlen, my_bool mod = false);
my_bool JsonInit(UDF_INIT* initid, UDF_ARGS* args, char* message, my_bool mbn,
unsigned long reslen, unsigned long memlen,
unsigned long more = 0);
my_bool CheckMemory(PGLOBAL g, UDF_INIT* initid, UDF_ARGS* args, uint n,
my_bool m, my_bool obj = false, my_bool mod = false);
PSZ MakePSZ(PGLOBAL g, UDF_ARGS* args, int i);
int IsArgJson(UDF_ARGS* args, uint i);
char *GetJsonFile(PGLOBAL g, char* fn);
/*********************************************************************************/
/* Structure JPN. Used to make the locate path. */
/*********************************************************************************/
typedef struct _jpn {
int Type;
PCSZ Key;
int N;
} JPN, *PJPN;
#endif // 0
/* --------------------------- New Testing BJSON Stuff --------------------------*/ /* --------------------------- New Testing BJSON Stuff --------------------------*/
extern uint JsonGrpSize;
uint GetJsonGroupSize(void);
typedef class BJNX* PBJNX; typedef class BJNX* PBJNX;
...@@ -19,11 +89,13 @@ typedef class BJNX* PBJNX; ...@@ -19,11 +89,13 @@ typedef class BJNX* PBJNX;
class BJNX : public BDOC { class BJNX : public BDOC {
public: public:
// Constructors // Constructors
BJNX(PGLOBAL g);
BJNX(PGLOBAL g, PBVAL row, int type, int len = 64, int prec = 0, my_bool wr = false); BJNX(PGLOBAL g, PBVAL row, int type, int len = 64, int prec = 0, my_bool wr = false);
// Implementation // Implementation
int GetPrecision(void) { return Prec; } int GetPrecision(void) { return Prec; }
PVAL GetValue(void) { return Value; } PVAL GetValue(void) { return Value; }
void SetRow(PBVAL vp) { Row = vp; }
// Methods // Methods
my_bool SetJpath(PGLOBAL g, char* path, my_bool jb = false); my_bool SetJpath(PGLOBAL g, char* path, my_bool jb = false);
...@@ -32,9 +104,16 @@ class BJNX : public BDOC { ...@@ -32,9 +104,16 @@ class BJNX : public BDOC {
PBVAL GetRowValue(PGLOBAL g, PBVAL row, int i, my_bool b = true); PBVAL GetRowValue(PGLOBAL g, PBVAL row, int i, my_bool b = true);
PBVAL GetJson(PGLOBAL g); PBVAL GetJson(PGLOBAL g);
my_bool CheckPath(PGLOBAL g); my_bool CheckPath(PGLOBAL g);
my_bool CheckPath(PGLOBAL g, UDF_ARGS* args, PBVAL jsp, PBVAL& jvp, int n);
my_bool WriteValue(PGLOBAL g, PBVAL jvalp); my_bool WriteValue(PGLOBAL g, PBVAL jvalp);
char* Locate(PGLOBAL g, PBVAL jsp, PBVAL jvp, int k = 1); char *Locate(PGLOBAL g, PBVAL jsp, PBVAL jvp, int k = 1);
char* LocateAll(PGLOBAL g, PBVAL jsp, PBVAL jvp, int mx = 10); char *LocateAll(PGLOBAL g, PBVAL jsp, PBVAL jvp, int mx = 10);
PSZ MakeKey(UDF_ARGS* args, int i);
PBVAL MakeBinValue(PGLOBAL g, UDF_ARGS* args, uint i);
PBVAL MakeValue(PGLOBAL g, UDF_ARGS* args, uint i, PBVAL* top = NULL);
PBVAL MakeTypedValue(PGLOBAL g, UDF_ARGS* args, uint i,
JTYP type, PBVAL* top = NULL);
PBVAL ParseJsonFile(PGLOBAL g, char* fn, int& pty, size_t& len);
protected: protected:
my_bool SetArrayOptions(PGLOBAL g, char* p, int i, PSZ nm); my_bool SetArrayOptions(PGLOBAL g, char* p, int i, PSZ nm);
...@@ -99,6 +178,14 @@ extern "C" { ...@@ -99,6 +178,14 @@ extern "C" {
DllExport char* bson_array_add_values(UDF_EXEC_ARGS); DllExport char* bson_array_add_values(UDF_EXEC_ARGS);
DllExport void bson_array_add_values_deinit(UDF_INIT*); DllExport void bson_array_add_values_deinit(UDF_INIT*);
DllExport my_bool bson_array_add_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_array_add(UDF_EXEC_ARGS);
DllExport void bson_array_add_deinit(UDF_INIT*);
DllExport my_bool bson_array_delete_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_array_delete(UDF_EXEC_ARGS);
DllExport void bson_array_delete_deinit(UDF_INIT*);
DllExport my_bool bsonlocate_init(UDF_INIT*, UDF_ARGS*, char*); DllExport my_bool bsonlocate_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bsonlocate(UDF_EXEC_ARGS); DllExport char* bsonlocate(UDF_EXEC_ARGS);
DllExport void bsonlocate_deinit(UDF_INIT*); DllExport void bsonlocate_deinit(UDF_INIT*);
...@@ -107,6 +194,104 @@ extern "C" { ...@@ -107,6 +194,104 @@ extern "C" {
DllExport char* bson_locate_all(UDF_EXEC_ARGS); DllExport char* bson_locate_all(UDF_EXEC_ARGS);
DllExport void bson_locate_all_deinit(UDF_INIT*); DllExport void bson_locate_all_deinit(UDF_INIT*);
DllExport my_bool bson_contains_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport long long bson_contains(UDF_INIT*, UDF_ARGS*, char*, char*);
DllExport void bson_contains_deinit(UDF_INIT*);
DllExport my_bool bsoncontains_path_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport long long bsoncontains_path(UDF_INIT*, UDF_ARGS*, char*, char*);
DllExport void bsoncontains_path_deinit(UDF_INIT*);
DllExport my_bool bson_make_object_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_make_object(UDF_EXEC_ARGS);
DllExport void bson_make_object_deinit(UDF_INIT*);
DllExport my_bool bson_object_nonull_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_object_nonull(UDF_EXEC_ARGS);
DllExport void bson_object_nonull_deinit(UDF_INIT*);
DllExport my_bool bson_object_key_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_object_key(UDF_EXEC_ARGS);
DllExport void bson_object_key_deinit(UDF_INIT*);
DllExport my_bool bson_object_add_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_object_add(UDF_EXEC_ARGS);
DllExport void bson_object_add_deinit(UDF_INIT*);
DllExport my_bool bson_object_delete_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_object_delete(UDF_EXEC_ARGS);
DllExport void bson_object_delete_deinit(UDF_INIT*);
DllExport my_bool bson_object_list_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_object_list(UDF_EXEC_ARGS);
DllExport void bson_object_list_deinit(UDF_INIT*);
DllExport my_bool bson_object_values_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_object_values(UDF_EXEC_ARGS);
DllExport void bson_object_values_deinit(UDF_INIT*);
DllExport my_bool bson_item_merge_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_item_merge(UDF_EXEC_ARGS);
DllExport void bson_item_merge_deinit(UDF_INIT*);
DllExport my_bool bson_get_item_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bson_get_item(UDF_EXEC_ARGS);
DllExport void bson_get_item_deinit(UDF_INIT*);
DllExport my_bool bsonget_string_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bsonget_string(UDF_EXEC_ARGS);
DllExport void bsonget_string_deinit(UDF_INIT*);
DllExport my_bool bsonget_int_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport long long bsonget_int(UDF_INIT*, UDF_ARGS*, char*, char*);
DllExport void bsonget_int_deinit(UDF_INIT*);
DllExport my_bool bsonget_real_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport double bsonget_real(UDF_INIT*, UDF_ARGS*, char*, char*);
DllExport void bsonget_real_deinit(UDF_INIT*);
DllExport my_bool bsonset_grp_size_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport long long bsonset_grp_size(UDF_INIT*, UDF_ARGS*, char*, char*);
DllExport my_bool bsonget_grp_size_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport long long bsonget_grp_size(UDF_INIT*, UDF_ARGS*, char*, char*);
DllExport my_bool bson_array_grp_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport void bson_array_grp_clear(UDF_INIT *, char *, char *);
DllExport void bson_array_grp_add(UDF_INIT *, UDF_ARGS *, char *, char *);
DllExport char *bson_array_grp(UDF_EXEC_ARGS);
DllExport void bson_array_grp_deinit(UDF_INIT*);
DllExport my_bool bson_object_grp_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport void bson_object_grp_clear(UDF_INIT *, char *, char *);
DllExport void bson_object_grp_add(UDF_INIT *, UDF_ARGS *, char *, char *);
DllExport char *bson_object_grp(UDF_EXEC_ARGS);
DllExport void bson_object_grp_deinit(UDF_INIT*);
DllExport my_bool bson_set_item_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *bson_set_item(UDF_EXEC_ARGS);
DllExport void bson_set_item_deinit(UDF_INIT*);
DllExport my_bool bson_insert_item_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *bson_insert_item(UDF_EXEC_ARGS);
DllExport void bson_insert_item_deinit(UDF_INIT*);
DllExport my_bool bson_update_item_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *bson_update_item(UDF_EXEC_ARGS);
DllExport void bson_update_item_deinit(UDF_INIT*);
DllExport my_bool bson_file_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *bson_file(UDF_EXEC_ARGS);
DllExport void bson_file_deinit(UDF_INIT*);
DllExport my_bool bfile_make_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bfile_make(UDF_EXEC_ARGS);
DllExport void bfile_make_deinit(UDF_INIT*);
DllExport my_bool bfile_convert_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bfile_convert(UDF_EXEC_ARGS);
DllExport void bfile_convert_deinit(UDF_INIT*);
DllExport my_bool bfile_bjson_init(UDF_INIT*, UDF_ARGS*, char*); DllExport my_bool bfile_bjson_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char* bfile_bjson(UDF_EXEC_ARGS); DllExport char* bfile_bjson(UDF_EXEC_ARGS);
DllExport void bfile_bjson_deinit(UDF_INIT*); DllExport void bfile_bjson_deinit(UDF_INIT*);
......
...@@ -812,7 +812,7 @@ int DOSFAM::ReadBuffer(PGLOBAL g) ...@@ -812,7 +812,7 @@ int DOSFAM::ReadBuffer(PGLOBAL g)
p = To_Buf + strlen(To_Buf) - 1; p = To_Buf + strlen(To_Buf) - 1;
if (trace(2)) if (trace(2))
htrc(" Read: To_Buf=%p p=%c\n", To_Buf, To_Buf, p); htrc(" Read: To_Buf=%p p=%c\n", To_Buf, p);
#if defined(__WIN__) #if defined(__WIN__)
if (Bin) { if (Bin) {
......
...@@ -35,7 +35,7 @@ static PJSON JsonNew(PGLOBAL g, JTYP type); ...@@ -35,7 +35,7 @@ static PJSON JsonNew(PGLOBAL g, JTYP type);
static PJVAL JvalNew(PGLOBAL g, JTYP type, void *vp = NULL); static PJVAL JvalNew(PGLOBAL g, JTYP type, void *vp = NULL);
static PJSNX JsnxNew(PGLOBAL g, PJSON jsp, int type, int len = 64); static PJSNX JsnxNew(PGLOBAL g, PJSON jsp, int type, int len = 64);
static uint JsonGrpSize = 10; uint JsonGrpSize = 10;
/*********************************************************************************/ /*********************************************************************************/
/* SubAlloc a new JSNX class with protection against memory exhaustion. */ /* SubAlloc a new JSNX class with protection against memory exhaustion. */
...@@ -1166,7 +1166,7 @@ static void SetChanged(PBSON bsp) ...@@ -1166,7 +1166,7 @@ static void SetChanged(PBSON bsp)
/*********************************************************************************/ /*********************************************************************************/
/* Replaces GetJsonGrpSize not usable when CONNECT is not installed. */ /* Replaces GetJsonGrpSize not usable when CONNECT is not installed. */
/*********************************************************************************/ /*********************************************************************************/
static uint GetJsonGroupSize(void) uint GetJsonGroupSize(void)
{ {
return (JsonGrpSize) ? JsonGrpSize : GetJsonGrpSize(); return (JsonGrpSize) ? JsonGrpSize : GetJsonGrpSize();
} // end of GetJsonGroupSize } // end of GetJsonGroupSize
...@@ -5837,11 +5837,11 @@ my_bool jfile_convert_init(UDF_INIT* initid, UDF_ARGS* args, char* message) { ...@@ -5837,11 +5837,11 @@ my_bool jfile_convert_init(UDF_INIT* initid, UDF_ARGS* args, char* message) {
} // endif args } // endif args
CalcLen(args, false, reslen, memlen); CalcLen(args, false, reslen, memlen);
return JsonInit(initid, args, message, false, reslen, memlen); return JsonInit(initid, args, message, true, reslen, memlen);
} // end of jfile_convert_init } // end of jfile_convert_init
char *jfile_convert(UDF_INIT* initid, UDF_ARGS* args, char* result, char *jfile_convert(UDF_INIT* initid, UDF_ARGS* args, char* result,
unsigned long *res_length, char *, char *error) { unsigned long *res_length, char *is_null, char *error) {
char *str, *fn, *ofn; char *str, *fn, *ofn;
int lrecl = (int)*(longlong*)args->args[2]; int lrecl = (int)*(longlong*)args->args[2];
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
...@@ -5853,20 +5853,21 @@ char *jfile_convert(UDF_INIT* initid, UDF_ARGS* args, char* result, ...@@ -5853,20 +5853,21 @@ char *jfile_convert(UDF_INIT* initid, UDF_ARGS* args, char* result,
if (!g->Xchk) { if (!g->Xchk) {
JUP* jup = new(g) JUP(g); JUP* jup = new(g) JUP(g);
str = strcpy(result, jup->UnprettyJsonFile(g, fn, ofn, lrecl)); str = jup->UnprettyJsonFile(g, fn, ofn, lrecl);
g->Xchk = str; g->Xchk = str;
} else } else
str = (char*)g->Xchk; str = (char*)g->Xchk;
if (!str) { if (!str) {
if (g->Message) PUSH_WARNING(g->Message ? g->Message : "Unexpected error");
str = strcpy(result, g->Message); *is_null = 1;
else *error = 1;
str = strcpy(result, "Unexpected error"); *res_length = 0;
} else {
} // endif str strcpy(result, str);
*res_length = strlen(str);
} // endif str
*res_length = strlen(str);
return str; return str;
} // end of jfile_convert } // end of jfile_convert
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment