From 2653bd3e4403a1ec608c655520ae7dc77e364845 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 22 Dec 2015 07:31:44 -0800 Subject: [PATCH] Struct field access must be mindful of alignments. --- Execute.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Execute.cpp b/Execute.cpp index 6bf9b50..6581ee1 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -890,6 +890,11 @@ static bool CString_setProperty(JSContextRef context, JSObjectRef object, JSStri return true; } CYCatch(false) } +template +static void Align(Type_ &data, size_t size) { + data = reinterpret_cast((reinterpret_cast(data) + (size - 1)) & ~(size - 1)); +} + static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { Type_privateData *typical(internal->type_); sig::Type *type(typical->type_); @@ -927,9 +932,12 @@ static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *inter ffi_type **elements(typical->GetFFI()->elements); base = reinterpret_cast(internal->value_); - for (ssize_t local(0); local != index; ++local) + for (ssize_t local(0); local != index; ++local) { + Align(base, elements[local]->alignment); base += elements[local]->size; + } + Align(base, elements[index]->alignment); return true; } -- 2.47.2