From: Dmitry Volyntsev Date: Fri, 22 May 2026 03:31:42 +0000 (-0700) Subject: Fixed Buffer float access alignment X-Git-Tag: 1.0.0~50 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/postgres_fdw.c?a=commitdiff_plain;h=b9825cd829e4e77e08219c9e30562c9e402ea20f;p=njs.git Fixed Buffer float access alignment --- diff --git a/src/njs_buffer.c b/src/njs_buffer.c index bd987c5f..49ead277 100644 --- a/src/njs_buffer.c +++ b/src/njs_buffer.c @@ -1225,7 +1225,7 @@ njs_buffer_prototype_read_float(njs_vm_t *vm, njs_value_t *args, switch (size) { case 4: - u32 = *((uint32_t *) u8); + u32 = njs_get_u32(u8); if (swap) { u32 = njs_bswap_u32(u32); @@ -1237,7 +1237,7 @@ njs_buffer_prototype_read_float(njs_vm_t *vm, njs_value_t *args, case 8: default: - conv_f64.u = *((uint64_t *) u8); + conv_f64.u = njs_get_u64(u8); if (swap) { conv_f64.u = njs_bswap_u64(conv_f64.u); @@ -1569,7 +1569,7 @@ njs_buffer_prototype_write_float(njs_vm_t *vm, njs_value_t *args, conv_f32.u = njs_bswap_u32(conv_f32.u); } - *((uint32_t *) u8) = conv_f32.u; + njs_set_u32(u8, conv_f32.u); break; case 8: @@ -1580,7 +1580,7 @@ njs_buffer_prototype_write_float(njs_vm_t *vm, njs_value_t *args, conv_f64.u = njs_bswap_u64(conv_f64.u); } - *((uint64_t *) u8) = conv_f64.u; + njs_set_u64(u8, conv_f64.u); } njs_set_number(retval, index + size); diff --git a/src/qjs_buffer.c b/src/qjs_buffer.c index aa597764..2243822d 100644 --- a/src/qjs_buffer.c +++ b/src/qjs_buffer.c @@ -1123,7 +1123,7 @@ qjs_buffer_prototype_read_float(JSContext *ctx, JSValueConst this_val, switch (size) { case 4: - u32 = *((uint32_t *) &self.start[index]); + u32 = njs_get_u32(&self.start[index]); if (swap) { u32 = njs_bswap_u32(u32); @@ -1135,7 +1135,7 @@ qjs_buffer_prototype_read_float(JSContext *ctx, JSValueConst this_val, case 8: default: - u64 = *((uint64_t *) &self.start[index]); + u64 = njs_get_u64(&self.start[index]); if (swap) { u64 = njs_bswap_u64(u64); diff --git a/test/buffer.t.js b/test/buffer.t.js index 71aa73a2..d02dca1f 100644 --- a/test/buffer.t.js +++ b/test/buffer.t.js @@ -692,6 +692,24 @@ let readFloat_tsuite = { throw Error(`unexpected output "${b.readFloatBE(0)}" != "123.125"`); } + r = b.writeFloatLE(123.125, 1); + if (r !== 5) { + throw Error(`unexpected output "${r}" != "5"`); + } + + if (b.readFloatLE(1) !== 123.125) { + throw Error(`unexpected output "${b.readFloatLE(1)}" != "123.125"`); + } + + r = b.writeFloatBE(123.125, 1); + if (r !== 5) { + throw Error(`unexpected output "${r}" != "5"`); + } + + if (b.readFloatBE(1) !== 123.125) { + throw Error(`unexpected output "${b.readFloatBE(1)}" != "123.125"`); + } + r = b.writeDoubleLE(123.125, 1); if (r !== 9) { throw Error(`unexpected output "${r}" != "9"`);