Skip to content

Commit a321d59

Browse files
committed
More size limit fixes
1 parent 0e9430c commit a321d59

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

doc/Usage.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ sys_string result = S("a") + U'b' + "cd" + "ef"s + u"gh"sv + U"ij" + std::vector
142142
assert(result == S("abcdefghijkl"));
143143
```
144144
145-
[!WARNING]
146-
147-
You must not use `auto` for the addition result. The result of an addition is a special temporary that only performs actual concatenation when converted to `sys_string_t`. Using `auto` declares a variable of that temporary type which will result in dangling pointers.
145+
> [!WARNING]
146+
> You must not use `auto` to declare the addition result. The result of an addition is a special temporary that only performs actual concatenation when converted to `sys_string_t`. Using `auto` declares a variable of that temporary type which will result in dangling pointers.
148147
149148
```cpp
150149
auto res = sys_string("abc") + sys_string("xyz"); //Bad!
@@ -527,8 +526,8 @@ friend auto operator<<(std::ostream & str, const sys_string & val) -> std::ostre
527526

528527
Prints the string content **as UTF-8** into `std::ostream`.
529528

530-
[!WARNING]
531-
Note that if your output goes somewhere that doesn't use UTF-8 encoding (Windows console or Unix terminal with non UTF-8 locale, for example) the output will be garbled.
529+
> [!WARNING]
530+
> Note that if your output goes somewhere that doesn't use UTF-8 encoding (Windows console or Unix terminal with non UTF-8 locale, for example) the output will be garbled.
532531
533532
On Windows or on any platform that defines `__STDC_ISO_10646__` macro there is also
534533

@@ -538,9 +537,9 @@ friend auto operator<<(std::wostream & str, const sys_string & val) -> std::wost
538537

539538
That prints out UTF-16 or UTF-32 views of the string.
540539

541-
[!NOTE]
542-
The `operator<<` currently completely ignores width, precision and fill settings of the stream - the entire string
543-
is printed out as is. This might change in future versions.
540+
> [!NOTE]
541+
> The `operator<<` currently completely ignores width, precision and fill settings of the stream - the entire string
542+
> is printed out as is. This might change in future versions.
544543
545544
### Formatting with `std::format`
546545

lib/inc/sys_string/impl/addition.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace sysstr::util
6868
auto storage_size() const -> typename sys_string_t<Storage>::size_type
6969
{
7070
size_t count = m_view.size();
71-
if (count > size_t(std::numeric_limits<typename sys_string_t<Storage>::size_type>::max()))
71+
if (count > size_t(Storage::max_size))
7272
throw std::bad_alloc();
7373
return static_cast<typename sys_string_t<Storage>::size_type>(count);
7474
}
@@ -112,7 +112,7 @@ namespace sysstr::util
112112
utf_encoding_of<typename sys_string_t<Storage>::storage_type>>;
113113
count = converter::converted_length(this->m_range);
114114
}
115-
if (count > size_t(std::numeric_limits<typename sys_string_t<Storage>::size_type>::max()))
115+
if (count > size_t(Storage::max_size))
116116
throw std::bad_alloc();
117117
return static_cast<typename sys_string_t<Storage>::size_type>(count);
118118
}
@@ -181,7 +181,7 @@ namespace sysstr::util
181181
{
182182
auto s1 = this->m_first.storage_size();
183183
auto s2 = this->m_second.storage_size();
184-
if (std::numeric_limits<typename sys_string_t<Storage>::size_type>::max() - s1 < s2)
184+
if (Storage::max_size - s1 < s2)
185185
throw std::bad_alloc();
186186
return s1 + s2;
187187
}

lib/inc/sys_string/impl/builder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace sysstr
5252
{
5353
if constexpr (std::ranges::contiguous_range<Range>)
5454
{
55-
impl.append(std::ranges::data(range), std::ranges::size(range));
55+
impl.append(std::ranges::data(range), limit_size(std::ranges::size(range)));
5656
}
5757
else
5858
{
@@ -78,7 +78,7 @@ namespace sysstr
7878
{
7979
if constexpr (std::ranges::contiguous_range<Range>)
8080
{
81-
return impl.insert(where, std::ranges::data(range), std::ranges::size(range));
81+
return impl.insert(where, std::ranges::data(range), limit_size(std::ranges::size(range)));
8282
}
8383
else
8484
{

lib/inc/sys_string/sys_string.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,12 @@ namespace sysstr
660660
{ return m_impl; }
661661

662662
private:
663+
static size_type limit_size(size_t len)
664+
{
665+
if (len > size_t(Storage::max_size))
666+
throw std::bad_alloc();
667+
return size_type(len);
668+
}
663669
static void append_one(impl_type & impl, char32_t c);
664670

665671
static typename impl_type::iterator insert_one(impl_type & impl, typename impl_type::iterator where, char32_t c);

0 commit comments

Comments
 (0)