From 427175b9c03c5d564ae2459e2e2abc3640bbd52b Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 29 Jan 2017 16:11:29 -0500 Subject: [PATCH 1/2] Added an extra flag to avoid potential race condition on is_full_, being reset from the background despite a write area not having been allocated. --- Outputs/CRT/Internals/TextureBuilder.cpp | 5 ++++- Outputs/CRT/Internals/TextureBuilder.hpp | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Outputs/CRT/Internals/TextureBuilder.cpp b/Outputs/CRT/Internals/TextureBuilder.cpp index 29c0f87b1..89efc616e 100644 --- a/Outputs/CRT/Internals/TextureBuilder.cpp +++ b/Outputs/CRT/Internals/TextureBuilder.cpp @@ -43,6 +43,7 @@ TextureBuilder::TextureBuilder(size_t bytes_per_pixel, GLenum texture_unit) : write_areas_start_y_(0), is_full_(false), did_submit_(false), + has_write_area_(false), number_of_write_areas_(0) { image_.resize(bytes_per_pixel * InputBufferBuilderWidth * InputBufferBuilderHeight); @@ -105,6 +106,7 @@ uint8_t *TextureBuilder::allocate_write_area(size_t required_length) else write_areas_.push_back(next_write_area); number_of_write_areas_++; + has_write_area_ = true; return pointer_to_location(next_write_area.x, next_write_area.y); } @@ -116,8 +118,9 @@ bool TextureBuilder::is_full() void TextureBuilder::reduce_previous_allocation_to(size_t actual_length) { - if(is_full_) return; + if(is_full_ || !has_write_area_) return; + has_write_area_ = false; WriteArea &write_area = write_areas_[number_of_write_areas_-1]; write_area.length = (uint16_t)actual_length; diff --git a/Outputs/CRT/Internals/TextureBuilder.hpp b/Outputs/CRT/Internals/TextureBuilder.hpp index d200a9b97..93777ac92 100644 --- a/Outputs/CRT/Internals/TextureBuilder.hpp +++ b/Outputs/CRT/Internals/TextureBuilder.hpp @@ -69,6 +69,7 @@ class TextureBuilder { size_t number_of_write_areas_; bool is_full_; bool did_submit_; + bool has_write_area_; inline uint8_t *pointer_to_location(uint16_t x, uint16_t y); // Usually: the start position for the current batch of write areas. From 38f944bc342a45939c39a625baa5d111b004f783 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 29 Jan 2017 16:12:48 -0500 Subject: [PATCH 2/2] This needs to be a `memmove` as the areas may overlap. --- Outputs/CRT/Internals/ArrayBuilder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Outputs/CRT/Internals/ArrayBuilder.cpp b/Outputs/CRT/Internals/ArrayBuilder.cpp index 0793dc372..afda1c9b6 100644 --- a/Outputs/CRT/Internals/ArrayBuilder.cpp +++ b/Outputs/CRT/Internals/ArrayBuilder.cpp @@ -130,7 +130,7 @@ void ArrayBuilder::Buffer::flush() { if(submitted_data) { - memcpy(data.data(), &data[submitted_data], allocated_data - submitted_data); + memmove(data.data(), &data[submitted_data], allocated_data - submitted_data); allocated_data -= submitted_data; flushed_data -= submitted_data; submitted_data = 0;