hyperrealm / libconfig

C/C++ library for processing configuration files
https://hyperrealm.github.io/libconfig/
GNU Lesser General Public License v2.1
1.12k stars 364 forks source link

Not finding settings that exist #245

Open ventsyv opened 2 months ago

ventsyv commented 2 months ago

A piece of code that works perfectly fine with libConfig 1.7.2 is failing to find the config settings when running with libConfig 1.7.3

Here is a simplified example. I haven't tested it (the example) but I'm fairly certain this will throw SettingNotFound exception:

bool myFunc(const libconfig::Settings& root)
{
    string name = "MySetting";
    string location = "";
    uint32_t value = 0;

    try
    {
        const libConfig::Settings& settings = root.lookup(location);
        if (! settings.lookupValue(name, value) )
            return false;
    }
    catch (libConfig::SettingNotFoundException& x)
    {
        //log
        return false;
    }
    catch (libConfig::SettingNotFoundException& x)
    {
        //log
        return false;
    }

    cout<<"Value is: " << value <<endl;

}

The config file looks like this:


#************ HEADER **********
# Some comment 1
# Some comment 2
#*******************************

# Another comment
MySetting = 25;

I'll try to debug it a bit more later and might be able to contribute a fix, but it will be appreciated if someone familiar with the code can look at a diff between the 2 versions and see if there is anything obvious. I looked at the diff myself but don't see anything that can cause this.

hyperrealm commented 2 months ago

You were relying on a bug that has been fixed. Empty string is not a valid path.

Your code (in the try block) should be:

value = root.lookup(name);