From f715cac6cf8d7e2660abf7f72173856a0b375ad8 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Tue, 2 May 2017 14:52:42 +0200 Subject: [PATCH] Fix bug reported by valgrind: if s1==s2, memcpy call is illegal (but unlikely to fail). --- m-string.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/m-string.h b/m-string.h index 0cf307c8..b00810f4 100644 --- a/m-string.h +++ b/m-string.h @@ -192,14 +192,16 @@ string_set (string_t v1, const string_t v2) { STRING_CONTRACT (v1); STRING_CONTRACT (v2); - size_t size = v2->size; - stringi_fit2size(v1, size+1); - if (M_LIKELY (v2->ptr != NULL)) { - memcpy(v1->ptr, v2->ptr, size+1); - } else { - v1->ptr[0] = 0; + if (v1 != v2) { + size_t size = v2->size; + stringi_fit2size(v1, size+1); + if (M_LIKELY (v2->ptr != NULL)) { + memcpy(v1->ptr, v2->ptr, size+1); + } else { + v1->ptr[0] = 0; + } + v1->size = size; } - v1->size = size; STRING_CONTRACT (v1); }