[gcc r16-9325] libstdc++: Provide defined behavior for unrecognized _Pres_type values.

Tomasz Kaminski via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <20260721081544.D5F8D4BA2E04__38546.0256178462$1784621753$gmane$org@sourceware.org>
https://gcc.gnu.org/g:0fe47b88b867510b02f95464f53c8e252c5d4dba

commit r16-9325-g0fe47b88b867510b02f95464f53c8e252c5d4dba
Author: Tomasz Kamiński <[email protected]>
Date:   Wed Jun 24 11:40:08 2026 +0200

    libstdc++: Provide defined behavior for unrecognized _Pres_type values.
    
    If the new _Pres_type values are introduced for given type, they may
    lead to unrecognized _Pres_type values, if the TU using them is
    linked with TU compiled with older releases, and format from old
    TU is selected.
    
    For most of the formatters, the default implementation is used as the
    fallback, however __formatter_int and __formatter_fp were treating
    that as UB, due to call to __builtin_unreachable in default branch
    the switch. This patch addresses above by fallbacking to _Pres_none
    behavior in such case.
    
    Note that this for C++20 affects programs using non-Unicode literal
    encoding, as __do_vformat_to is exported from the library otherwise,
    and thus newest version is always picked.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/format (__formatter_int::format)
            (__formatter_fp::format): For unrecognised _M_spec._M_type
            values (default branch of switch) fallthrou to _Pres_none.
    
    Reviewed-by: Jonathan Wakely <[email protected]>
    Signed-off-by: Tomasz Kamiński <[email protected]>
    (cherry picked from commit 4fd42fce8d43cefb59c61d2865c198c35c0860be)

Diff:
---
 libstdc++-v3/include/std/format | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 9cce822378d2..33af80afe957 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -1672,6 +1672,7 @@ namespace __format
 	    case _Pres_c:
 	      return _M_format_character(_S_to_character(__i), __fc);
 #endif
+	    default: // Fallback for _Pres_type values introduces in later versions.
 	    case _Pres_none:
 	      // Should not reach here with _Pres_none for bool or charT, so:
 	      [[fallthrough]];
@@ -1695,8 +1696,6 @@ namespace __format
 		  *__p = std::toupper(*__p);
 #endif
 	      break;
-	    default:
-	      __builtin_unreachable();
 	  }
 
 	  if (_M_spec._M_alt && __base_prefix.size())
@@ -2124,12 +2123,11 @@ namespace __format
 	      __use_prec = true;
 	      __fmt = chars_format::general;
 	      break;
+	    default: // Fallback for _Pres_type values introduces in later versions.
 	    case _Pres_none:
 	      if (__use_prec)
 		__fmt = chars_format::general;
 	      break;
-	    default:
-	      __builtin_unreachable();
 	  }
 
 	  // Write value into buffer using std::to_chars.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.