From 5402458ff5838fdad76fd7f8a901bf9682a739ad Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 12 Dec 2016 08:13:26 +0100 Subject: [PATCH] :ambulance: fixed an AddressSanitizer warning --- src/json.hpp | 25 ++++++++++++++----------- src/json.hpp.re2c | 25 ++++++++++++++----------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 450c6467..c15dc30a 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -6271,7 +6271,7 @@ class basic_json types. @param[in] vec byte vector to read from - @param[in] current_index the psition in the vector after which to read + @param[in] current_index the position in the vector after which to read @return the next sizeof(T) bytes from @a vec, in reverse order as T @@ -6280,20 +6280,24 @@ class basic_json @throw std::out_of_range if there are less than sizeof(T)+1 bytes in the vector @a vec to read + In the for loop, the bytes from the vector are copied in reverse order into + the return value. In the figures below, let sizeof(T)=4 and `i` be the loop + variable. + Precondition: - vec: | | | a | b | c | d | | | T: | | | | | - ^ ^ ^ ^ - current_index idx ptr sizeof(T) + vec: | | | a | b | c | d | T: | | | | | + ^ ^ ^ ^ + current_index i ptr sizeof(T) Postcondition: - vec: | | | a | b | c | d | | | T: | d | c | b | a | - ^ ^ ^ - | idx ptr + vec: | | | a | b | c | d | T: | d | c | b | a | + ^ ^ ^ + | i ptr current_index - @sa Code from . + @sa Code adapted from . */ template static T get_from_vector(const std::vector& vec, const size_t current_index) @@ -6305,10 +6309,9 @@ class basic_json T result; uint8_t* ptr = reinterpret_cast(&result); - size_t idx = current_index + 1 + sizeof(T); - while (idx > current_index) + for (size_t i = 0; i < sizeof(T); ++i) { - *ptr++ = vec[--idx]; + *ptr++ = vec[current_index + sizeof(T) - i]; } return result; } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index c381682c..1dc97bd6 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -6271,7 +6271,7 @@ class basic_json types. @param[in] vec byte vector to read from - @param[in] current_index the psition in the vector after which to read + @param[in] current_index the position in the vector after which to read @return the next sizeof(T) bytes from @a vec, in reverse order as T @@ -6280,20 +6280,24 @@ class basic_json @throw std::out_of_range if there are less than sizeof(T)+1 bytes in the vector @a vec to read + In the for loop, the bytes from the vector are copied in reverse order into + the return value. In the figures below, let sizeof(T)=4 and `i` be the loop + variable. + Precondition: - vec: | | | a | b | c | d | | | T: | | | | | - ^ ^ ^ ^ - current_index idx ptr sizeof(T) + vec: | | | a | b | c | d | T: | | | | | + ^ ^ ^ ^ + current_index i ptr sizeof(T) Postcondition: - vec: | | | a | b | c | d | | | T: | d | c | b | a | - ^ ^ ^ - | idx ptr + vec: | | | a | b | c | d | T: | d | c | b | a | + ^ ^ ^ + | i ptr current_index - @sa Code from . + @sa Code adapted from . */ template static T get_from_vector(const std::vector& vec, const size_t current_index) @@ -6305,10 +6309,9 @@ class basic_json T result; uint8_t* ptr = reinterpret_cast(&result); - size_t idx = current_index + 1 + sizeof(T); - while (idx > current_index) + for (size_t i = 0; i < sizeof(T); ++i) { - *ptr++ = vec[--idx]; + *ptr++ = vec[current_index + sizeof(T) - i]; } return result; }