[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cfe-commits
Subject:    [cfe-commits] Fwd: [llvm-commits] bug in libc++'s std::string::find_first_not_of
From:       David Blaikie <dblaikie () gmail ! com>
Date:       2012-12-31 19:01:07
Message-ID: CAENS6Euvc7oM2A+M9HBpN4NH888_djswoM4TAg=XrET-+wT+fw () mail ! gmail ! com
[Download RAW message or body]

Moving to the appropriate list (cfe-commits) & adding Howard.


---------- Forwarded message ----------
From: Klaas de Vries <klaas@klaasgaaf.nl>
Date: Mon, Dec 31, 2012 at 6:06 AM
Subject: [llvm-commits] bug in libc++'s std::string::find_first_not_of
To: llvm-commits@cs.uiuc.edu


Hi,

It seems that the libc++ implementation of
std::string::find_first_not_of - specifically the one taking a single
character as its first argument - does not handle the case where the
string contains only the given character (i.e. where it should return
string::npos).

A simple program demonstrating this:

int main()
{
  string test("--");
  string::size_type i = test.find_first_not_of('-');
  cout << (i == string::npos ? "okay" : "not okay") << '\n';
}

The cause seems to be a typo in the for-loop in the implementation
where a check for the inequality will return true for the first
character beyond the tested string (a bad thing).

Attached is what I believe to be a fix, and an updated test case.

Regards,
Klaas de Vries


_______________________________________________
llvm-commits mailing list
llvm-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

["find_first_not_of.diff" (text/x-patch)]

Index: include/string
===================================================================
--- include/string	(revision 171297)
+++ include/string	(working copy)
@@ -3374,7 +3374,7 @@
     {
         const_pointer __p = data();
         const_pointer __pe = __p + __sz;
-        for (const_pointer __ps = __p + __pos; __p != __pe; ++__ps)
+        for (const_pointer __ps = __p + __pos; __ps != __pe; ++__ps)
             if (!traits_type::eq(*__ps, __c))
                 return static_cast<size_type>(__ps - __p);
     }
Index: test/strings/basic.string/string.ops/string_find.first.not.of/char_size.pass.cpp
===================================================================
--- test/strings/basic.string/string.ops/string_find.first.not.of/char_size.pass.cpp	(revision 171297)
+++ test/strings/basic.string/string.ops/string_find.first.not.of/char_size.pass.cpp	(working copy)
@@ -59,6 +59,8 @@
     test(S("oselktgbcapndfjihrmq"), 'q', 21, S::npos);
 
     test(S(""), 'q', S::npos);
+    test(S("q"), 'q', S::npos);
+    test(S("qqq"), 'q', S::npos);
     test(S("csope"), 'q', 0);
     test(S("gfsmthlkon"), 'q', 0);
     test(S("laenfsbridchgotmkqpj"), 'q', 0);


_______________________________________________
cfe-commits mailing list
cfe-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic