The segmentation fault is caused in the scanner code.
The Problem:
Using GDB to backtrack reveals that the problem is caused with the declaration of the FieldInfo Pointer named field_info (where FieldInfo is a struct) in the condition: if (tell_me).
Please note the the following code is a part of a large file, so if there are some things whose declaration is not here, you can probably assume that they would have been defined in the program somewhere else and not shown here.
The sample code:
Some_function(some_arguments) {
// Did something.
if (flag_1) {
list<const FieldInfo *> prefix_stack;
const FieldInfo def_pfx(NON_BOOLEAN, default_prefix);
{
const FieldInfo * default_field_info = &def_pfx;
if (default_prefix.empty()) {
map<string, FieldInfo>::const_iterator f = field_map.find("");
if (f != field_map.end()) default_field_info = &(f->second);
}
// We always have the current prefix on the top of the stack.
prefix_stack.push_back(default_field_info);
}
// Did something.
for (<some conditions>) {
bool tell_me = false;
// Did something.
if (tell_me) {
const FieldInfo pos_prefix(NON_BOOLEAN, pos);
const FieldInfo * field_info = &pos_prefix;
Term * term_obj = new Term(&state, term_lowercase, field_info,
term, stem_term, term_pos++);
Parse(pParser, token, term_obj, &state);
} else {
const FieldInfo * field_info = prefix_stack.back();
Term * term_obj = new Term(&state, term_lowercase, field_info,
term, stem_term, term_pos++);
Parse(pParser, token, term_obj, &state);
}
// Did something.
}
}
// Did something.
}
And the definition of FieldInfo is:
struct FieldInfo {
/// The type of this field.
filter_type type;
/// Field prefix strings.
list<string> prefixes;
/// Field processors struct already defined earlier.
list<FieldProcessor*> procs;
FieldInfo(filter_type type_, const string & prefix)
: type(type_)
{
prefixes.push_back(prefix);
}
FieldInfo(filter_type type_, FieldProcessor *proc)
: type(type_)
{
procs.push_back(proc);
}
};
Analysis:
Parse is a method that calls the Parser.
GDB reveals that the problem (segmentation fault) is caused when the Parser tries to process the field_info by iterating over the field_info->prefixes.
EDIT:
Here is the code of the function where the segmentation fault occurs (I have added some cout for the debugging purposes). The problem comes is in the while (++piter != prefixes.end()) part of code:
Query get_query() const
{
const list<string> & prefixes = field_info->prefixes;
if (prefixes.empty()) {
assert(!field_info->procs.empty());
return (**field_info->procs.begin())(name);
}
list<string>::const_iterator piter = prefixes.begin();
Query q(make_term(*piter), 1, pos);
while (++piter != prefixes.end()) {
string check3 = make_term(*piter);
Query q2(check3, 1, pos);
q = Query(Query::OP_OR, q, q2);
}
return q;
}
NOTE:
I am working on some-one else’s working code.
I have added the if(flag_1) part of code, and rest everything else was there already.
This part looks suspicious:
You are using the address of the local variable
pos_prefixto initializeterm_obj. You have to make absolutely sure that this address is never accessed afterpos_prefixhas gone out of scope, because then the address will be invalid.You are having an awful lot of raw pointers in your code. This is not good practice in modern C++. Consider using plain objects, references or smart-pointers.