summaryrefslogtreecommitdiff
path: root/libcxx/test/std/strings/basic.string/string.modifiers/string_insert
AgeCommit message (Collapse)Author
2025-01-21[libc++] Fix input-only range handling for `basic_string` (#116890)A. Jiang
By calling `std::move` for related functions when the iterator is possibly input-only. Also slightly changes the conditions of branch for contiguous iterators to avoid error. Fixes #116502
2023-12-13[ASan][libc++] std::basic_string annotations (#72677)Tacet
This commit introduces basic annotations for `std::basic_string`, mirroring the approach used in `std::vector` and `std::deque`. Initially, only long strings with the default allocator will be annotated. Short strings (_SSO - short string optimization_) and strings with non-default allocators will be annotated in the near future, with separate commits dedicated to enabling them. The process will be similar to the workflow employed for enabling annotations in `std::deque`. **Please note**: these annotations function effectively only when libc++ and libc++abi dylibs are instrumented (with ASan). This aligns with the prevailing behavior of Memory Sanitizer. To avoid breaking everything, this commit also appends `_LIBCPP_INSTRUMENTED_WITH_ASAN` to `__config_site` whenever libc++ is compiled with ASan. If this macro is not defined, string annotations are not enabled. However, linking a binary that does **not** annotate strings with a dynamic library that annotates strings, is not permitted. Originally proposed here: https://reviews.llvm.org/D132769 Related patches on Phabricator: - Turning on annotations for short strings: https://reviews.llvm.org/D147680 - Turning on annotations for all allocators: https://reviews.llvm.org/D146214 This PR is a part of a series of patches extending AddressSanitizer C++ container overflow detection capabilities by adding annotations, similar to those existing in `std::vector` and `std::deque` collections. These enhancements empower ASan to effectively detect instances where the instrumented program attempts to access memory within a collection's internal allocation that remains unused. This includes cases where access occurs before or after the stored elements in `std::deque`, or between the `std::basic_string`'s size (including the null terminator) and capacity bounds. The introduction of these annotations was spurred by a real-world software bug discovered by Trail of Bits, involving an out-of-bounds memory access during the comparison of two strings using the `std::equals` function. This function was taking iterators (`iter1_begin`, `iter1_end`, `iter2_begin`) to perform the comparison, using a custom comparison function. When the `iter1` object exceeded the length of `iter2`, an out-of-bounds read could occur on the `iter2` object. Container sanitization, upon enabling these annotations, would effectively identify and flag this potential vulnerability. This Pull Request introduces basic annotations for `std::basic_string`. Long strings exhibit structural similarities to `std::vector` and will be annotated accordingly. Short strings are already implemented, but will be turned on separately in a forthcoming commit. Look at [a comment](https://github.com/llvm/llvm-project/pull/72677#issuecomment-1850554465) below to read about SSO issues at current moment. Due to the functionality introduced in [D132522](https://github.com/llvm/llvm-project/commit/dd1b7b797a116eed588fd752fbe61d34deeb24e4), the `__sanitizer_annotate_contiguous_container` function now offers compatibility with all allocators. However, enabling this support will be done in a subsequent commit. For the time being, only strings with the default allocator will be annotated. If you have any questions, please email: - advenam.tacet@trailofbits.com - disconnect3d@trailofbits.com
2023-12-12Add `std::basic_string` test cases (#74830)Tacet
Extend `std::basic_string` tests to cover more buffer situations and length in general, particularly non-SSO cases after SSO test cases (changing buffers). This commit is a side effect of working on tests for ASan annotations. Related PR: https://github.com/llvm/llvm-project/pull/72677
2023-11-26[libc++][test] Fix unused and nodiscard warnings (#73437)Stephan T. Lavavej
Found while running libc++'s test suite with MSVC's STL. This is structured into a series of commits for easier reviewing; I could also split this into smaller PRs if desired. * Add void-casts for `invoke_r` calls to fix MSVC STL `[[nodiscard]]` warnings. + Our rationale is that if someone is calling `invoke_r<NonVoidType>`, it sure looks like they care about the return value. * Add `[[maybe_unused]]` to silence `-Wunused-parameter` warnings. + This happens because the parameters are used within `LIBCPP_ASSERT`, which vanishes for MSVC's STL. This also motivates the following changes. * Add `[[maybe_unused]]` to fix `-Wunused-variable` warnings. * Always void-cast `debug_comparisons` to fix `-Wunused-variable` warnings. + As this was already unused with a void-cast in one `_LIBCPP_HARDENING_MODE` branch, I'm simply lifting it next to the variable definition. * Add `[[maybe_unused]]` to fix `-Wunused-local-typedef` warnings.
2023-09-27[libc++] Refactor string unit tests to ease addition of new allocatorsLouis Dionne
While doing this, I also found a few tests that were either clearly incorrect (e.g. testing the wrong function) or that lacked basic test coverage like testing std::string itself (e.g. the test was only checking std::basic_string with a custom allocator). In these cases, I did a few conservative drive-by changes. Differential Revision: https://reviews.llvm.org/D140550 Co-authored-by: Brendan Emery <brendan.emery@esrlabs.com>
2023-09-01[libc++] Apply clang formatting to all string unit testsBrendan Emery
This applies clang-format to the std::string unit tests in preparation for landing https://reviews.llvm.org/D140550. Differential Revision: https://reviews.llvm.org/D140612
2023-07-05[libc++][ranges] Implement the changes to `basic_string` from P1206 ↵varconst
(`ranges::to`): - add the `from_range_t` constructors and the related deduction guides; - add the `insert_range`/`assign_range`/etc. member functions. (Note: this patch is split from https://reviews.llvm.org/D142335) Differential Revision: https://reviews.llvm.org/D149832
2023-03-14[libc++] Qualifies uint32_t and friends.Mark de Wever
This has been done using the following command find libcxx/test -type f -exec perl -pi -e 's|^([^/]+?)((?<!::)(?<!::u)u?int(_[a-z]+)?[0-9]{1,2}_t)|\1std::\2|' \{} \; And manually removed some false positives in std/depr/depr.c.headers. Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D145880
2022-10-12[libc++] Add test for bug that had been introduced in D98573 and fixed in ↵Louis Dionne
D119633 Differential Revision: https://reviews.llvm.org/D135297
2022-08-26[libc++][NFC] Remove some of the code duplication in the string testsNikolas Klauser
Reviewed By: ldionne, #libc, huixie90 Spies: huixie90, libcxx-commits, arphaman Differential Revision: https://reviews.llvm.org/D131856
2022-06-13[libcxx][AIX] Switch build compiler to clangJake Egan
This patch switches the build compiler for AIX from ibm-clang to clang. ibm-clang++_r has `-pthread` by default, but clang for AIX doesn't, so `-pthread` had to be added to the test config. A bunch of tests now pass, so the `XFAIL` was removed. This patch also switch the build to use the visibility support available in clang-15 to control symbols exported by the shared library (AIX traditionally uses explicit export lists for this purpose). Reviewed By: #libc, #libc_abi, daltenty, #libunwind, ldionne Differential Revision: https://reviews.llvm.org/D127470
2022-04-27[libc++] Implement P0980R1 (constexpr std::string)Nikolas Klauser
Reviewed By: #libc, ldionne Spies: daltenty, sdasgup3, ldionne, arichardson, MTC, ChuanqiXu, mehdi_amini, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes, tatianashp, rdzhabarov, teijeong, cota, dcaballe, Chia-hungDuan, wrengr, wenzhicui, arphaman, Mordante, miscco, Quuxplusone, smeenai, libcxx-commits Differential Revision: https://reviews.llvm.org/D110598
2022-02-10[libc++] Prepare string.modifiers tests for constexprNikolas Klauser
Reviewed By: ldionne, #libc Spies: libcxx-commits Differential Revision: https://reviews.llvm.org/D119329
2022-01-04[libc++] Remove incorrect default constructor in cpp17_input_iteratorLouis Dionne
AFAICT, Cpp17InputIterators are not required to be default constructible, since that requirement is added in Cpp17ForwardIterator. Hence, our archetype for Cpp17InputIterator should not be default constructible. Removing that constructor has a ripple effect on a couple of tests that were making incorrect assumptions. Notably: - Some tests were using cpp17_input_iterator as a sentinel for itself. That is not valid, because a cpp17_input_iterator is not semiregular anymore after the change (and hence it doesn't satisfy sentinel_for). - Some tests were using a stride-counted cpp17_input_iterator as the sentinel for a range. This doesn't work anymore because of the problem above, so these tests were changed not to check stride counts for input iterators. - Some tests were default constructing cpp17_input_iterator when a simple alternative was available -- those have been changed to use that alternative. Differential Revision: https://reviews.llvm.org/D115806
2021-05-02[libcxx][nfc] prefixes test type `input_iterator` with `cpp17_`Christopher Di Bella
C++20 revised the definition of what it means to be an iterator. While all _Cpp17InputIterators_ satisfy `std::input_iterator`, the reverse isn't true. D100271 introduces a new test adaptor to accommodate this new definition (`cpp20_input_iterator`). In order to help readers immediately distinguish which input iterator adaptor is _Cpp17InputIterator_, the current `input_iterator` adaptor has been prefixed with `cpp17_`. Differential Revision: https://reviews.llvm.org/D101242
2021-04-26[libc++] [test] Improve test_exceptions() in each string.modifiers test.Arthur O'Dwyer
When checking the strong exception guarantee, also check that iterators haven't been invalidated. Reviewed as part of https://reviews.llvm.org/D98573
2021-04-26[libc++] Remove the special logic for "noexcept iterators" in basic_string.Arthur O'Dwyer
This reverts a large chunk of http://reviews.llvm.org/D15862 , and also fixes bugs in `insert`, `append`, and `assign`, which are now regression-tested. (Thanks to Tim Song for pointing out the bug in `append`!) Before this patch, we did a special dance in `append`, `assign`, and `insert` (but not `replace`). All of these require the strong exception guarantee, even when the user-provided InputIterator might have throwing operations. The naive way to accomplish this is to construct a temporary string and then append/assign/insert from the temporary; i.e., finish all the potentially throwing and self-inspecting InputIterator operations *before* starting to modify self. But this is slow, so we'd like to skip it when possible. The old code (D15682) attempted to check that specific iterator operations were nothrow: it assumed that if the iterator operations didn't throw, then it was safe to iterate the input range multiple times and therefore it was safe to use the fast-path non-naive version. This was wrong for two reasons: (1) the old code checked the wrong operations (e.g. checked noexceptness of `==`, but the code that ran used `!=`), and (2) the conversion of value_type to char could still throw, or inspect the contents of self. The new code is much simpler, although still much more complicated than it really could be. We'll likely revisit this codepath at some point, but for now this patch suffices to get it passing all the new regression tests. The added tests all fail before this patch, and succeed afterward. See https://quuxplusone.github.io/blog/2021/04/17/pathological-string-appends/ Differential Revision: https://reviews.llvm.org/D98573
2020-10-05[libc++] Fix several debug mode testsLouis Dionne
These tests were only being run when _LIBCPP_DEBUG was defined, which isn't the case by default when we run the test suite. In other words, all these debug mode tests were never being run. This commit makes sure they are run, and in some cases, extracts them into a file under test/libcxx to separate them from the Standard tests. Differential Revision: https://reviews.llvm.org/D88836
2020-06-03[libc++] Remove the c++98 Lit feature from the test suiteLouis Dionne
C++98 and C++03 are effectively aliases as far as Clang is concerned. As such, allowing both std=c++98 and std=c++03 as Lit parameters is just slightly confusing, but provides no value. It's similar to allowing both std=c++17 and std=c++1z, which we don't do. This was discovered because we had an internal bot that ran the test suite under both c++98 AND c++03 -- one of which is redundant. Differential Revision: https://reviews.llvm.org/D80926
2020-04-10[libc++] NFC: Clean up a lot of old Lit featuresLouis Dionne
The libc++ test suite has a lot of old Lit features used to XFAIL tests and mark them as UNSUPPORTED. Many of them are to workaround problems on old compilers or old platforms. As time goes by, it is good to go and clean those up to simplify the configuration of the test suite, and also to reflect the testing reality. It's not useful to have markup that gives the impression that e.g. clang-3.3 is supported, when we don't really test on it anymore (and hence several new tests probably don't have the necessary markup on them).
2019-05-31Add include for 'test_macros.h' to all the tests that were missing them. ↵Marshall Clow
Thanks to Zoe for the (big, but simple) patch. NFC intended. llvm-svn: 362252
2019-02-27[libc++] Mark several tests as XFAIL on macosx10.7Louis Dionne
Those tests fail when linking against a new dylib but running against macosx10.7. I believe this is caused by a duplicate definition of the RTTI for exception classes in libc++.dylib and libc++abi.dylib, but this matter still needs some investigation. This issue was not caught previously because all the tests always linked against the same dylib used for running (because LIT made it impossible to do otherwise before r349171). rdar://problem/46809586 llvm-svn: 354940
2019-02-04Support tests in freestandingJF Bastien
Summary: Freestanding is *weird*. The standard allows it to differ in a bunch of odd manners from regular C++, and the committee would like to improve that situation. I'd like to make libc++ behave better with what freestanding should be, so that it can be a tool we use in improving the standard. To do that we need to try stuff out, both with "freestanding the language mode" and "freestanding the library subset". Let's start with the super basic: run the libc++ tests in freestanding, using clang as the compiler, and see what works. The easiest hack to do this: In utils/libcxx/test/config.py add: self.cxx.compile_flags += ['-ffreestanding'] Run the tests and they all fail. Why? Because in freestanding `main` isn't special. This "not special" property has two effects: main doesn't get mangled, and main isn't allowed to omit its `return` statement. The first means main gets mangled and the linker can't create a valid executable for us to test. The second means we spew out warnings (ew) and the compiler doesn't insert the `return` we omitted, and main just falls of the end and does whatever undefined behavior (if you're luck, ud2 leading to non-zero return code). Let's start my work with the basics. This patch changes all libc++ tests to declare `main` as `int main(int, char**` so it mangles consistently (enabling us to declare another `extern "C"` main for freestanding which calls the mangled one), and adds `return 0;` to all places where it was missing. This touches 6124 files, and I apologize. The former was done with The Magic Of Sed. The later was done with a (not quite correct but decent) clang tool: https://gist.github.com/jfbastien/793819ff360baa845483dde81170feed This works for most tests, though I did have to adjust a few places when e.g. the test runs with `-x c`, macros are used for main (such as for the filesystem tests), etc. Once this is in we can create a freestanding bot which will prevent further regressions. After that, we can start the real work of supporting C++ freestanding fairly well in libc++. <rdar://problem/47754795> Reviewers: ldionne, mclow.lists, EricWF Subscribers: christof, jkorous, dexonsmith, arphaman, miyuki, libcxx-commits Differential Revision: https://reviews.llvm.org/D57624 llvm-svn: 353086
2019-01-19Update more file headers across all of the LLVM projects in the monorepoChandler Carruth
to reflect the new license. These used slightly different spellings that defeated my regular expressions. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351648
2018-07-02Implement LWG 2946, 3075 and 3076. Reviewed as https://reviews.llvm.org/D48616Marshall Clow
llvm-svn: 336132
2017-09-26[libcxx] [test] Strip trailing whitespace.Stephan T. Lavavej
llvm-svn: 314258
2017-09-07Add more string_view testsMarshall Clow
llvm-svn: 312690
2017-07-29[libcxx] [test] Untabify, NFC.Stephan T. Lavavej
llvm-svn: 309464
2017-04-19Cleanup _LIBCPP_HAS_NO_<c++11-feature> in the string library.Eric Fiselier
llvm-svn: 300633
2017-04-15Fix PR32642 - string::insert and string::append don't work with move_iterator.Eric Fiselier
llvm-svn: 300397
2016-11-23[libcxx] [test] D27027: Strip trailing whitespace.Stephan T. Lavavej
llvm-svn: 287829
2016-11-01Protect exceptional paths under libcpp-no-exceptionsRoger Ferrer Ibanez
These tests are of the form try { action-that-may-throw assert(!exceptional-condition) assert(some-other-facts) } catch (relevant-exception) { assert(exceptional-condition) } Under libcpp-no-exceptions there is still value in verifying some-other-facts while avoiding the exceptional case. So for these tests just conditionally check some-other-facts if exceptional-condition is false. When exception are supported make sure that a true exceptional-condition throws an exception Differential Revision: https://reviews.llvm.org/D26136 llvm-svn: 285697
2016-09-24Fix a few static_asserts that need extra parens on -03Marshall Clow
llvm-svn: 282343
2016-09-24Implement proposed resolution for LWG#2758. Reviewed as D24446. Normally, I ↵Marshall Clow
would wait for these to be voted upon at a committee meeting (November), but the current draft standard is broken, and this should fix it. (And if it doesn't, we want to know about it soonest) llvm-svn: 282342
2016-09-07Fix PR#30303 - no matching function for call to '__ptr_in_range'Marshall Clow
llvm-svn: 280779
2016-09-05Fix Bug 30240 - std::string: append(first, last) error when aliasing. Add ↵Marshall Clow
test cases for append/insert/assign/replace while we're at it, and fix a similar bug in insert. llvm-svn: 280643
2016-04-28Guard libc++ specific c.__invariants() tests in LIBCPP_ASSERT macrosEric Fiselier
llvm-svn: 267947
2016-01-20More string fixes for noexcept cases. Apparently I didn't get them all in ↵Marshall Clow
r258281. llvm-svn: 258291
2016-01-20Fix up the tests I added for string exceptions to be skipped when exceptions ↵Marshall Clow
are disabled llvm-svn: 258279
2016-01-13Fix PR#25973 : 'basic_string::assign(InputIt, InputIt) doesn't provide the ↵Marshall Clow
strong exception safety guarantee'. This turned out to be a pervasive problem in <string>, which required a fair amount of rework. Add in an optimization for when iterators provide noexcept increment/comparison/assignment/dereference (which covers many of the iterators in libc++). Reviewed as http://reviews.llvm.org/D15862 llvm-svn: 257682
2015-11-10Make it possible to build a no-exceptions variant of libcxx.Asiri Rathnayake
Fixes a small omission in libcxx that prevents libcxx being built when -DLIBCXX_ENABLE_EXCEPTIONS=0 is specified. This patch adds XFAILS to all those tests that are currently failing on the new -fno-exceptions library variant. Follow-up patches will update the tests (progressively) to cope with the new library variant. Change-Id: I4b801bd8d8e4fe7193df9e55f39f1f393a8ba81a llvm-svn: 252598
2014-12-20Move test into test/std subdirectory.Eric Fiselier
llvm-svn: 224658