Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: use std::list::sort() to avoid iterator be invalid #145

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Conzxy
Copy link

@Conzxy Conzxy commented Apr 7, 2023

When configuration is in MSVC Debug mode, I call the replxx_history_save(), following code will cause crash with message:
list iterators is incompatible .

void History::erase( entries_t::iterator it_ ) {
	bool invalidated( it_ == _current );
[...]
}

I find the key code as following:

bool History::save( std::string const& filename, bool sync_ ) {
[...]
	entries_t entries;
	locations_t locations;
	if ( ! sync_ ) {
		entries.swap( _entries );
		locations.swap( _locations );
		_entries = entries;
		reset_iters();
	}
	/* scope for ifstream object auto-close */ {
		ifstream histFile( filename );
		if ( histFile ) {
			do_load( histFile );
		}
	}
	sort(); // all list iterators have been invalid
	remove_duplicates(); // _current is no changed
	trim_to_max_size(); // call erase()
	ofstream histFile( filename );
	if ( ! histFile ) {
		return ( false );
	}
[...]
}
[...]
void History::trim_to_max_size( void ) {
	while ( size() > _maxSize ) {
		erase( _entries.begin() );
	}
}

Here, trim_to_max_size call the History::erase(), the _entries.begin() from current list but _current from previous _entries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant