mirror of
https://github.com/ultrajson/ultrajson.git
synced 2024-05-29 02:26:09 +02:00
memory alignment fixes
Avoids segmentation faults on strict architectures e.g. SPARC
This commit is contained in:
parent
c4016cfcda
commit
6901f41a7c
|
@ -149,7 +149,7 @@ typedef void * JSITER;
|
|||
typedef struct __JSONTypeContext
|
||||
{
|
||||
int type;
|
||||
void *prv[32];
|
||||
void *prv;
|
||||
} JSONTypeContext;
|
||||
|
||||
/*
|
||||
|
|
|
@ -110,7 +110,6 @@ void Buffer_Realloc (JSONObjectEncoder *enc, size_t cbNeeded)
|
|||
SetError (NULL, enc, "Could not reserve memory block");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -122,7 +121,6 @@ void Buffer_Realloc (JSONObjectEncoder *enc, size_t cbNeeded)
|
|||
SetError (NULL, enc, "Could not reserve memory block");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy (enc->start, oldStart, offset);
|
||||
}
|
||||
enc->offset = enc->start + offset;
|
||||
|
@ -265,6 +263,7 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char
|
|||
case 2:
|
||||
{
|
||||
JSUTF32 in;
|
||||
JSUTF16 in16;
|
||||
|
||||
if (end - io < 1)
|
||||
{
|
||||
|
@ -273,7 +272,8 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
in = *((JSUTF16 *) io);
|
||||
memcpy(&in16, io, sizeof(JSUTF16));
|
||||
in = (JSUTF32) in16;
|
||||
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
ucs = ((in & 0x1f) << 6) | ((in >> 8) & 0x3f);
|
||||
|
@ -295,6 +295,8 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char
|
|||
case 3:
|
||||
{
|
||||
JSUTF32 in;
|
||||
JSUTF16 in16;
|
||||
JSUINT8 in8;
|
||||
|
||||
if (end - io < 2)
|
||||
{
|
||||
|
@ -303,13 +305,15 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
memcpy(&in16, io, sizeof(JSUTF16));
|
||||
memcpy(&in8, io + 2, sizeof(JSUINT8));
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
in = *((JSUTF16 *) io);
|
||||
in |= *((JSUINT8 *) io + 2) << 16;
|
||||
in = (JSUTF32) in16;
|
||||
in |= in8 << 16;
|
||||
ucs = ((in & 0x0f) << 12) | ((in & 0x3f00) >> 2) | ((in & 0x3f0000) >> 16);
|
||||
#else
|
||||
in = *((JSUTF16 *) io) << 8;
|
||||
in |= *((JSUINT8 *) io + 2);
|
||||
in = in16 << 8;
|
||||
in |= in8;
|
||||
ucs = ((in & 0x0f0000) >> 4) | ((in & 0x3f00) >> 2) | (in & 0x3f);
|
||||
#endif
|
||||
|
||||
|
@ -335,11 +339,10 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
memcpy(&in, io, sizeof(JSUTF32));
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
in = *((JSUTF32 *) io);
|
||||
ucs = ((in & 0x07) << 18) | ((in & 0x3f00) << 4) | ((in & 0x3f0000) >> 10) | ((in & 0x3f000000) >> 24);
|
||||
#else
|
||||
in = *((JSUTF32 *) io);
|
||||
ucs = ((in & 0x07000000) >> 6) | ((in & 0x3f0000) >> 4) | ((in & 0x3f00) >> 2) | (in & 0x3f);
|
||||
#endif
|
||||
if (ucs < 0x10000)
|
||||
|
@ -801,7 +804,6 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName)
|
|||
enc->endTypeContext(obj, &tc);
|
||||
return;
|
||||
}
|
||||
|
||||
Buffer_AppendCharUnchecked (enc, '\"');
|
||||
|
||||
|
||||
|
@ -863,7 +865,6 @@ char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *_buffer, size_t
|
|||
SetError(obj, enc, "Could not reserve memory block");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
enc->heap = 1;
|
||||
}
|
||||
else
|
||||
|
@ -883,7 +884,6 @@ char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *_buffer, size_t
|
|||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Buffer_AppendCharUnchecked(enc, '\0');
|
||||
|
||||
return enc->start;
|
||||
|
|
|
@ -409,25 +409,32 @@ char *Dict_iterGetName(JSOBJ obj, JSONTypeContext *tc, size_t *outLen)
|
|||
|
||||
void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc)
|
||||
{
|
||||
PyObject *exc, *toDictFunc;
|
||||
PyObject* obj = (PyObject*) _obj;
|
||||
TypeContext *pc = (TypeContext *) tc->prv;
|
||||
PyObject *obj, *exc, *toDictFunc;
|
||||
TypeContext *pc;
|
||||
PRINTMARK();
|
||||
if (!_obj) {
|
||||
tc->type = JT_INVALID;
|
||||
return;
|
||||
}
|
||||
|
||||
tc->prv[0] = 0;
|
||||
tc->prv[1] = 0;
|
||||
tc->prv[2] = 0;
|
||||
tc->prv[3] = 0;
|
||||
tc->prv[4] = 0;
|
||||
tc->prv[5] = 0;
|
||||
tc->prv[6] = 0;
|
||||
tc->prv[7] = 0;
|
||||
tc->prv[8] = 0;
|
||||
tc->prv[9] = 0;
|
||||
tc->prv[10] = 0;
|
||||
tc->prv[11] = 0;
|
||||
tc->prv[12] = 0;
|
||||
tc->prv[13] = 0;
|
||||
tc->prv[14] = 0;
|
||||
obj = (PyObject*) _obj;
|
||||
|
||||
tc->prv = PyObject_Malloc(sizeof(TypeContext));
|
||||
pc = (TypeContext *) tc->prv;
|
||||
if (!pc)
|
||||
{
|
||||
tc->type = JT_INVALID;
|
||||
PyErr_NoMemory();
|
||||
return;
|
||||
}
|
||||
pc->newObj = NULL;
|
||||
pc->dictObj = NULL;
|
||||
pc->itemValue = NULL;
|
||||
pc->itemName = NULL;
|
||||
pc->attrList = NULL;
|
||||
pc->index = 0;
|
||||
pc->size = 0;
|
||||
pc->longValue = 0;
|
||||
|
||||
if (PyIter_Check(obj))
|
||||
{
|
||||
|
@ -453,8 +460,7 @@ void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc)
|
|||
if (exc && PyErr_ExceptionMatches(PyExc_OverflowError))
|
||||
{
|
||||
PRINTMARK();
|
||||
tc->type = JT_INVALID;
|
||||
return;
|
||||
goto INVALID;
|
||||
}
|
||||
|
||||
return;
|
||||
|
@ -600,12 +606,21 @@ ISITERABLE:
|
|||
pc->iterGetName = Dir_iterGetName;
|
||||
|
||||
return;
|
||||
|
||||
INVALID:
|
||||
tc->type = JT_INVALID;
|
||||
PyObject_Free(tc->prv);
|
||||
tc->prv = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void Object_endTypeContext(JSOBJ obj, JSONTypeContext *tc)
|
||||
{
|
||||
Py_XDECREF(GET_TC(tc)->newObj);
|
||||
|
||||
PyObject_Free(tc->prv);
|
||||
tc->prv = NULL;
|
||||
}
|
||||
|
||||
const char *Object_getStringValue(JSOBJ obj, JSONTypeContext *tc, size_t *_outLen)
|
||||
|
|
Loading…
Reference in New Issue