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