From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id E94013858C98; Thu, 4 Apr 2024 15:40:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E94013858C98 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=googlemail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E94013858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::42e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712245233; cv=none; b=xvDMWSVym2aNU83RA6dRwnh2GbUvTj/xDvwlS1w24J/PadGDoFaOUmtdb2phPcNbGlrSzFTEV9hbcNDqNLpyOYZK9D9sdEywwLjOHvD/lBSoUmro170z9IJo5Z7JVFgf6rx+IT9xOP1addBs23KPtq5rxt2CEY2HxmL3iKXr8O8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712245233; c=relaxed/simple; bh=eDEgQQtAE1aVvWdneZm9u4fG+ssvz1VcYVL/kTsg3AI=; h=DKIM-Signature:Mime-Version:Subject:From:Date:Message-Id:To; b=KsEr2eAWKljRBvaZLO4pnXiqnNntxIOJLoot3k7onFeuWzdtLlGbUQPxF5GL9mGsgxp23XnDqyllXk/+SsdXhxtccptPRT6PidU/RHhpzCm8qWkKX7kieJY+GjpKjz1YU9yAmay8tbg+SYJC9mnvL7Rmspyy80B66+OHVS4mElI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-3439ce73766so603141f8f.2; Thu, 04 Apr 2024 08:40:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1712245221; x=1712850021; darn=gcc.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=uNtURWwAcLKI+RNoKiLVouy48wUFXbHCP3VW9XuCCEk=; b=J8x2yjbI88K+++yH9h5Cbkuq3wQQDTZ78d4NtnXcpvaM6RF1cCLPB7UmgmyoauwX0B FW0u1tIy2FgY6xev0N5qgEYnfhAoQGfzzIQ/1CMSRTIk6aB1WqtJc5Lg8EEWerad5xSn 15pnEUeUfjgttb++usUN97pMHm19LHmURuLRHaYFk9YwfsolOHduog82S2G4foGfBFPC X0AIpmqpa0HY+1sVyelGWigSoKtgropmBDRBrdMWW01b2igYhwAb/ArxD7soK6zZ05oG CWBYscfcQp/01CnLbawr13lNQZMg1NcLB4Kk5XYRPPk/8DD0EFrMySZom5CHAVT4rtff NWoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712245221; x=1712850021; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uNtURWwAcLKI+RNoKiLVouy48wUFXbHCP3VW9XuCCEk=; b=Y6dDX5wXSgYK9ui2o9I2gMF5gvrpNbqAdViCP4IFgxLlb82DKfPOGOVIZRJKjWPEiq yqspAb7lvq24fH4isBwICI4GekYw1l/vIoRA5o5Uo9F1wxURJfyNa5NaGPSDxRsD3fOw WNRStlkkMBqFRZD327ZxfsvsC47q4wHd0yZe03n93w/SuFxwlWzrcgDECJKP+ZZOYlBW 1jXrphbTzu2J0sA0KM5OKD3qL9Pgu9KnOXJcgRqyquWMYYDZzxHFwFWxHBdw2SWTIJzt kwdVrkJt9haytm7WDINBTZhnrGfGe69/GZzyMRFv6Wqw4fYEPrBFIKrWdSG+KPHxxDTj mKBA== X-Forwarded-Encrypted: i=1; AJvYcCXn1OhWT6KqMeHmYPa0vjb1eXf5RF7PXZmMxrDo9kzj7TDlzLLebaN7h0OgGUMcZcoag2mrSriE00WPZSV+JiMPiGUDCbNwDw== X-Gm-Message-State: AOJu0YyNOqYfYanNxY4P5CHVnrYlrScO5EoXjvrPluYBEVrao4HHvp0s Qf52B3onVP/xn2QFwR/AKgQWFAE2sSdo9ffv18QSBR+D4Ao4D5p1 X-Google-Smtp-Source: AGHT+IE0Qdwu1DEhuMsvgnHUM2+sqtHF1PYVWcg2oxqtTP+sL0/0nBLambZINLvC4x21uiJ4/lla4Q== X-Received: by 2002:adf:9bc7:0:b0:343:8455:58d6 with SMTP id e7-20020adf9bc7000000b00343845558d6mr2265049wrc.26.1712245221349; Thu, 04 Apr 2024 08:40:21 -0700 (PDT) Received: from smtpclient.apple (host81-138-1-83.in-addr.btopenworld.com. [81.138.1.83]) by smtp.googlemail.com with ESMTPSA id bq24-20020a5d5a18000000b0033e45930f35sm20775152wrb.6.2024.04.04.08.40.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Apr 2024 08:40:20 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.8\)) Subject: Re: [PATCH] libstdc++: Fix infinite loop in std::istream::ignore(n, delim) [PR93672] From: Iain Sandoe In-Reply-To: <20240404153158.313297-1-jwakely@redhat.com> Date: Thu, 4 Apr 2024 16:40:20 +0100 Cc: libstdc++ , GCC Patches Content-Transfer-Encoding: quoted-printable Message-Id: <19633866-184F-4E13-B05B-C3473946E2B9@googlemail.com> References: <20240404153158.313297-1-jwakely@redhat.com> To: Jonathan Wakely X-Mailer: Apple Mail (2.3696.120.41.1.8) X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: > On 4 Apr 2024, at 16:29, Jonathan Wakely wrote: >=20 > I would appreciate more eyes on this to confirm my conclusions about > negative int_type values, and the proposed fix, make sense. >=20 > Tested x86_64-linux. >=20 > -- >8 -- >=20 > A negative value for the delim value passed to std::istream::ignore = can > never match any character in the stream, because the comparison is = done > using traits_type::eq_int_type(sb->sgetc(), delim) and sgetc() never > returns negative values (except at EOF). The optimized version of > ignore for the std::istream specialization uses traits_type::find to > locate the delim character in the streambuf, which _can_ match a > negative delim on platforms where char is signed, but then we do = another > comparison using eq_int_type which fails. The code then keeps looping > forever, with traits_type::find saying the character is present and > eq_int_type saying it's not. >=20 > A possible fix would be to check with eq_int_type after a successful > find, to see whether we really have a match. However, that would be > suboptimal since we know that a negative delimiter will never match > using eq_int_type. So a better fix is to adjust the check at the top = of > the function that handles delim=3D=3Deof(), so that we treat all = negative > delim values as equivalent to EOF. That way we don't bother using find > to search for something that will never match with eq_int_type. Is the corollary to this that a platform with signed chars can never use = a negative value as a delimiter - since that we always be treated as EOF? - I am not sure it there=E2=80=99s an actual use-case where that = matters, but, Iain >=20 > The version of ignore in the primary template doesn't need a change, > because it doesn't use traits_type::find, instead characters are > extracted one-by-one and always matched using eq_int_type. That avoids > the inconsistency between find and eq_int_type. >=20 > libstdc++-v3/ChangeLog: >=20 > PR libstdc++/93672 > * src/c++98/istream.cc (istream::ignore(streamsize, int_type)): > Treat all negative delimiter values as eof(). > * testsuite/27_io/basic_istream/ignore/char/93672.cc: New test. > --- > libstdc++-v3/src/c++98/istream.cc | 5 ++++- > .../27_io/basic_istream/ignore/char/93672.cc | 15 +++++++++++++++ > 2 files changed, 19 insertions(+), 1 deletion(-) > create mode 100644 = libstdc++-v3/testsuite/27_io/basic_istream/ignore/char/93672.cc >=20 > diff --git a/libstdc++-v3/src/c++98/istream.cc = b/libstdc++-v3/src/c++98/istream.cc > index 07ac739c26a..aa1069dea07 100644 > --- a/libstdc++-v3/src/c++98/istream.cc > +++ b/libstdc++-v3/src/c++98/istream.cc > @@ -112,7 +112,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > basic_istream:: > ignore(streamsize __n, int_type __delim) > { > - if (traits_type::eq_int_type(__delim, traits_type::eof())) > + // sgetc() returns either (int_type)(unsigned char)c or -1 for = EOF. > + // If __delim is negative, then eq_int_type(sgetc(), __delim) = can only > + // be true for EOF, so just treat all negative values as eof(). > + if (__delim < 0) > return ignore(__n); >=20 > _M_gcount =3D 0; > diff --git = a/libstdc++-v3/testsuite/27_io/basic_istream/ignore/char/93672.cc = b/libstdc++-v3/testsuite/27_io/basic_istream/ignore/char/93672.cc > new file mode 100644 > index 00000000000..6d11f5622c8 > --- /dev/null > +++ b/libstdc++-v3/testsuite/27_io/basic_istream/ignore/char/93672.cc > @@ -0,0 +1,15 @@ > +// { dg-do run } > + > +#include > +#include > + > +int main() > +{ > + std::istringstream in("x\xfdxxx\xfex"); > + in.ignore(10, std::char_traits::to_int_type('\xfd')); > + VERIFY( in.gcount() =3D=3D 2 ); > + VERIFY( ! in.eof() ); > + in.ignore(10, '\xfe'); > + VERIFY( in.gcount() =3D=3D 5 ); > + VERIFY( in.eof() ); > +} > --=20 > 2.44.0 >=20