std::nth_element crash problem
(1) Source code:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>auto less_compare = [] (const MirroringGroup& mg1, const MirroringGroup& mg2) -> bool {<br /> </li><li>return (mg1.usage() < mg2.usage());<br /></li><li>};<br /></li><li>std::nth_element(mgs->begin(), mgs->begin() + (copy_count - 1), mgs->end(), less_compare); </li></ol>
(2) Problem:
crash often occurs, The stack is as follows:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>#0 0x00000000004b3807 in MirroringGroup::CopyFrom (this=0x15edf20, from=...) at miuifs/miuistorage-dev/idl/proto/InternalData.pb.cc:6487<br /> </li><li>#1 0x000000000052bc71 in MirroringGroup::operator= (this=0x15edf20, from=...) at miuifs/miuistorage-dev/idl/proto/InternalData.pb.h:1797<br /></li><li>#2 0x000000000052f7cb in std::swap<MirroringGroup> (__a=..., __b=...) at /usr/local/include/c++/4.8.2/bits/move.h:177<br /></li><li>#3 0x000000000052e0b0 in std::iter_swap<__gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, __gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > > > (__a=..., __b=...)<br /></li><li>at /usr/local/include/c++/4.8.2/bits/stl_algobase.h:147<br /></li><li>#4 0x0000000000604b11 in std::__unguarded_partition<__gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup> >, MirroringGroup, miuifs::BlockManager::ChooseWritableMirroringGroups(std::vector<MirroringGroup>*, int)::__lambda101>(__gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, __gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, const MirroringGroup &, miuifs::BlockManager::__lambda101) (__first=..., __last=..., __pivot=..., __comp=...) at /usr/local/include/c++/4.8.2/bits/stl_algo.h:2270<br /></li><li>#5 0x0000000000603c1b in std::__unguarded_partition_pivot<__gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup> >, miuifs::BlockManager::ChooseWritableMirroringGroups(std::vector<MirroringGroup>*, int)::__lambda101>(__gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, __gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, miuifs::BlockManager::__lambda101) (<br /></li><li>__first=..., __last=..., __comp=...) at /usr/local/include/c++/4.8.2/bits/stl_algo.h:2296<br /></li><li>#6 0x0000000000603408 in std::__introselect<__gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup> >, long int, miuifs::BlockManager::ChooseWritableMirroringGroups(std::vector<MirroringGroup>*, int)::__lambda101>(__gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, __gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, __gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, long, miuifs::BlockManager::__lambda101) (__first=..., __nth=..., __last=..., __depth_limit=2, <br /></li><li>__comp=...) at /usr/local/include/c++/4.8.2/bits/stl_algo.h:2394<br /></li><li>#7 0x0000000000602c95 in std::nth_element<__gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup> >, miuifs::BlockManager::ChooseWritableMirroringGroups(std::vector<MirroringGroup>*, int)::__lambda101>(__gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, __gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, __gnu_cxx::__normal_iterator<MirroringGroup*, std::vector<MirroringGroup, std::allocator<MirroringGroup> > >, miuifs::BlockManager::__lambda101) (__first=..., __nth=..., __last=..., __comp=...)<br /></li><li>at /usr/local/include/c++/4.8.2/bits/stl_algo.h:5417<br /></li><li>#8 0x000000000060039c in miuifs::BlockManager::ChooseWritableMirroringGroups (this=0x118abe0 <miuifs::BlockManager::Instance()::instance>, mgs=0x7fffeb9f4140, <br /></li><li>copy_count=2) at miuifs/miuistorage-dev/BlockManager.cc:391<br /></li><li>#9 0x00000000005ff9cf in miuifs::BlockManager::NewBlock (this=0x118abe0 <miuifs::BlockManager::Instance()::instance>) at miuifs/miuistorage-dev/BlockManager.cc:331<br /></li><li>#10 0x00000000005fed63 in miuifs::BlockManager::AcquireBlock (this=0x118abe0 <miuifs::BlockManager::Instance()::instance>, attribute=...)<br /></li><li>at miuifs/miuistorage-dev/BlockManager.cc:243</li></ol>
(3) Find the problem:
The problem always appears in std::nth_element. I didn’t think it was an STL problem at first, and there has been no good solution. Later, by reading the STL source code, I found the reason in /usr/local/include/c /4.8.2/bits/stl_algo.h:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>template<typename _RandomAccessIterator, typename _Compare><br /> </li><li>inline _RandomAccessIterator<br /></li><li>__unguarded_partition_pivot(_RandomAccessIterator __first,<br /></li><li>_RandomAccessIterator __last, _Compare __comp)<br /></li><li>{<br /></li><li>_RandomAccessIterator __mid = __first + (__last - __first) / 2;<br /></li><li>std::__move_median_to_first(__first, __first + 1, __mid, (__last - 2),<br /></li><li>__comp);<br /></li><li>return std::__unguarded_partition(__first + 1, __last, *__first, __comp);<br /></li><li>} </li></ol>
The function of __move_median_to_first function is to change __first 1, __mid, ( __last - 2) The middle-sized value is exchanged with __first. However, it ignores the situation where __mid,(__last - 2) points to the same iterator. If the input is as follows:

The result after __move_median_to_first is as follows:

At this time, __first points to the maximum value. Then look at the implementation of std::__unguarded_partition. In line 2263, __comp(*__first, __pivot)) always returns true, causing __first to be executed all the time and illegal memory is accessed.
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>template<typename _RandomAccessIterator, typename _Tp, typename _Compare><br /> </li><li>_RandomAccessIterator<br /></li><li>__unguarded_partition(_RandomAccessIterator __first,<br /></li><li>_RandomAccessIterator __last,<br /></li><li>const _Tp& __pivot, _Compare __comp)<br /></li><li>{<br /></li><li>while (true)<br /></li><li>{<br /></li><li>while (__comp(*__first, __pivot))<br /></li><li>++__first;<br /></li><li>--__last;<br /></li><li>while (__comp(__pivot, *__last))<br /></li><li>--__last;<br /></li><li>if (!(__first < __last))<br /></li><li>return __first;<br /></li><li>std::iter_swap(__first, __last);<br /></li><li>++__first;<br /></li><li>}<br /></li><li>} </li></ol>
(4) Solution:
I found the following link through Google and found that it is indeed an STL bug, which can only be solved by upgrading C.
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=732042

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software