https://gcc.gnu.org/g:959a80a46dbc4d3ad1bf8560dfacb585ccd8cac4

commit r15-6222-g959a80a46dbc4d3ad1bf8560dfacb585ccd8cac4
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Fri Dec 13 10:54:29 2024 +0000

    libstdc++: Fix uninitialized data in std::basic_spanbuf::seekoff
    
    I noticed a -Wmaybe-uninitialized warning for this function, which turns
    out to be correct. If the caller passes a valid std::ios_base::seekdir
    value then there's no problem, but if they pass std::seekdir(999) then
    we don't initialize the __base variable before adding it to __off.
    
    Rather than initialize it to an arbitrary value, we should return an
    error.
    
    Also add [[unlikely]] attributes to the paths that return an error.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/spanstream (basic_spanbuf::seekoff): Return an
            error for invalid seekdir values.

Diff:
---
 libstdc++-v3/include/std/spanstream | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/include/std/spanstream 
b/libstdc++-v3/include/std/spanstream
index 98ad3fa856a7..23a340a746e8 100644
--- a/libstdc++-v3/include/std/spanstream
+++ b/libstdc++-v3/include/std/spanstream
@@ -168,7 +168,7 @@ template<typename _CharT, typename _Traits>
        }
       else
        {
-         off_type __base;
+         off_type __base{};
          __which &= (ios_base::in|ios_base::out);
 
          if (__which == ios_base::out)
@@ -182,11 +182,13 @@ template<typename _CharT, typename _Traits>
            }
          else if (__way == ios_base::end)
            __base = _M_buf.size();
+         else /* way is not ios::beg, ios::cur, or ios::end */ [[unlikely]]
+           return __ret;
 
-         if (__builtin_add_overflow(__base, __off, &__off))
+         if (__builtin_add_overflow(__base, __off, &__off)) [[unlikely]]
            return __ret;
 
-         if (__off < 0 || __off > _M_buf.size())
+         if (__off < 0 || __off > _M_buf.size()) [[unlikely]]
            return __ret;
 
          if (__which & ios_base::in)

Reply via email to