b98e6f
From daf4ef50c88c2b9a6bf2c40b537eebc202caad6e Mon Sep 17 00:00:00 2001
b98e6f
From: =?UTF-8?q?S=C3=A9bastien=20Gonzalve?=
b98e6f
 <sebastien.gonzalve@aliceadsl.fr>
b98e6f
Date: Sat, 14 Nov 2020 10:39:47 +0100
b98e6f
Subject: [PATCH] Do not try to access element when vector is empty
b98e6f
b98e6f
Trying to access tmp[0] causes a crash on Fedora when assertion on STL
b98e6f
are enabled.
b98e6f
b98e6f
/usr/include/c++/10/bits/stl_vector.h:1045: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = unsigned char; _Alloc = std::allocator<unsigned char>; std::vector<_Tp, _Alloc>::reference = unsigned char&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]: Assertion '__builtin_expect(__n < this->size(), true)' failed.
b98e6f
b98e6f
This patch just passes nullptr as pointer to getSortKey() when tmp size
b98e6f
is 0, preventing dereferencing elements in empty vector.
b98e6f
b98e6f
I guess that &tmp[0] should be optimized as 'no real access' when
b98e6f
disabling assertion, but actually leads to crash when assert are
b98e6f
enabled.
b98e6f
---
b98e6f
 src/icu/collator.cpp | 2 +-
b98e6f
 1 file changed, 1 insertion(+), 1 deletion(-)
b98e6f
b98e6f
diff --git a/libs/locale/src/icu/collator.cpp b/libs/locale/src/icu/collator.cpp
b98e6f
index 7f1ea6a..dc59e8c 100644
b98e6f
--- a/libs/locale/src/icu/collator.cpp
b98e6f
+++ b/libs/locale/src/icu/collator.cpp
b98e6f
@@ -93,7 +93,7 @@ namespace boost {
b98e6f
                     std::vector<uint8_t> tmp;
b98e6f
                     tmp.resize(str.length());
b98e6f
                     icu::Collator *collate = get_collator(level);
b98e6f
-                    int len = collate->getSortKey(str,&tmp[0],tmp.size());
b98e6f
+                    int len = collate->getSortKey(str,tmp.empty()?nullptr:&tmp[0],tmp.size());
b98e6f
                     if(len > int(tmp.size())) {
b98e6f
                         tmp.resize(len);
b98e6f
                         collate->getSortKey(str,&tmp[0],tmp.size());
b98e6f
-- 
b98e6f
2.26.2
b98e6f