From 27f0d5fcc3e870d6bbf0a9e094ab814167b984ba Mon Sep 17 00:00:00 2001 From: marcizhu Date: Fri, 9 Dec 2016 18:36:40 +0100 Subject: [PATCH] Fixed float and double reading/writing bug in Buffer --- Cereal/Cereal/src/Buffer.h | 4 ++-- Cereal/Cereal/src/Reader.h | 6 +++--- Sandbox/Sandbox.cpp | 2 ++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cereal/Cereal/src/Buffer.h b/Cereal/Cereal/src/Buffer.h index b49068d..b1f949b 100644 --- a/Cereal/Cereal/src/Buffer.h +++ b/Cereal/Cereal/src/Buffer.h @@ -147,7 +147,7 @@ namespace Cereal { { unsigned int x; - *(unsigned int*)&x = (unsigned int)data; + *(unsigned int*)&x = *(unsigned int*)&data; writeBytes(x); @@ -159,7 +159,7 @@ namespace Cereal { { unsigned long long x; - *(unsigned long long*)&x = (unsigned long long)data; + *(unsigned long long*)&x = *(unsigned long long*)&data; writeBytes(x); diff --git a/Cereal/Cereal/src/Reader.h b/Cereal/Cereal/src/Reader.h index 0828cff..0607025 100644 --- a/Cereal/Cereal/src/Reader.h +++ b/Cereal/Cereal/src/Reader.h @@ -50,7 +50,7 @@ namespace Cereal { float result; - memcpy_s(&result, 4, &value, 4); + memcpy_s(&result, sizeof(float), &value, sizeof(float)); return result; } @@ -63,13 +63,13 @@ namespace Cereal { { unsigned long long value = src[pointer] << (sizeof(int) * 8 - 8); - for (int i = pointer; i < (int)pointer + (int) sizeof(float); i++) + for (int i = 0; i < (int) sizeof(double); i++) { value |= (src[pointer + i] << ((sizeof(int) * 8 - 8) - (i * 8))); } double result; - memcpy_s(&result, 4, &value, 4); + memcpy_s(&result, sizeof(double), &value, sizeof(double)); return result; } diff --git a/Sandbox/Sandbox.cpp b/Sandbox/Sandbox.cpp index dc55e28..faf51be 100644 --- a/Sandbox/Sandbox.cpp +++ b/Sandbox/Sandbox.cpp @@ -7,6 +7,8 @@ #include +#include + void gotoxy(int x, int y) { CONSOLE_SCREEN_BUFFER_INFO SBInfo;