Skip to content

unexpected result when calling method value() for negative integers #2

@gizero

Description

@gizero

Hi,

I noticed a problem in the way value() is implemented in pylibconfig. I added a simple testcase to your tests.py which highlight the problem. See below the trivial change to test script.

If I'm getting it right, the point here is that value() will return a datum of the first type which makes lookupValue() return true. Looking at libconfig's implementation of cast operator to unsigned int, it appear that negative integers will make lookupValue() return true with a data value clipped to zero. Hence detecting data type by trials with lookupValue() will fail in this case.

Here is how libconfig overloads cast to unsigned int:

Setting::operator unsigned int() const throw(SettingTypeException)
{

  assertType(TypeInt);

  int v = config_setting_get_int(_setting);

  if(v < 0)

    v = 0;

  return(static_cast<unsigned int>(v));

}

Here is the patch to make the test suite fail for the given case:

diff --git a/tests/test.py b/tests/test.py
index 66b373c..4d8447a 100644
--- a/tests/test.py
+++ b/tests/test.py
@@ -9,22 +9,29 @@ class PyLibConfigTest ( unittest.TestCase ):
         config = Config ()

         config.addString ( "", "test" )
         config.setValue ( "test", "value" )
+        config.addInteger ( "", "testInt" )
+        config.setValue ( "testInt", -4 )

         self.assert_ ( config.value ( "test" )[0] == "value" )
         self.assert_ ( config.value ( "test" )[1] == True )
         self.assert_ ( config.value ( "loose" )[1] == False )
         self.assert_ ( config.value ( "test_bool" )[0] == True )
         config.setValue( "test_bool", False )
+        self.assert_ ( config.value ( "testInt" )[0] == -4 )

         config.writeFile ( "test.conf" )

         self.assert_ ( os.path.exists ( "./test.conf" ) == True )

         config.readFile ( "test.conf" )

         self.assert_ ( config.value ( "test" )[0] == "value" )
         self.assert_ ( config.value ( "test" )[1] == True )
+        self.assert_ ( config.value ( "testInt" )[0] == -4 )

         os.remove ( "./test.conf" )
+
+if __name__ == '__main__':
+        unittest.main()

Am I getting it wrong?
Regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions