Overview
Features
Download
Documentation
Community
Add-Ons & Services

Bug in tryParse? (stores value even if parse error)

Please post support and help requests here.

Bug in tryParse? (stores value even if parse error)

Postby dslate » 09 May 2013, 18:36

I am a fairly new user of the POCO C++ libraries. So far, I have found them useful, but I seem to have run into a bug in tryParse. Here is a program (between the dotted lines) containing a test case. It should be fairly self-expanatory:

------------------------------------------------------------------------------------------------------------------------------------------------
// poco-tryParse-bug.c++: Example of POCO tryParse bug, which stores value even if parse fails.

// Built with clang++ version 3.3 (trunk), Target: x86_64-unknown-linux-gnu, Thread model: posix
// poco-1.4.6p1.tar.gz.
// OS: Ubuntu 12.04.2 LTS
// uname -a: Linux faith2 3.2.0-38-generic #61-Ubuntu SMP Tue Feb 19 12:18:21 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

// Compile command:
// clang++ -I/usr/local/include/Poco poco-tryParse-bug.c++ -o poco-tryParse-bug -lPocoFoundation -lPocoUtil

// Run command:
// ./poco-tryParse-bug

// Output I get:
// 0
// 382
// 1
// 597

// Documentation from 040-StringsAndFormatting.pdf:
// > bool tryParse(const std::string& str, int& value)
// Parses an integer value in decimal notation from str, and stores it
// in value. Returns true if the number is valid.
// Returns false, and leaves value unchanged, if not.

#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <Poco/NumberParser.h>
using namespace std;
using namespace Poco;

int main() {
int i = 0;
cout << NumberParser::tryParse( "382abc", i) << endl;
cout << i << endl;
i = 0;
cout << NumberParser::tryParse( "597", i) << endl;
cout << i << endl;
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------

According to the documentation, the first parse should fail and leave the value of i unchanged. It does fail, but still stores a value for i resulting from a parse of "382abc" up to the first invalid char. Can you reproduce my output, and if so, is this a bug, or am I just confused?

Thanks,

-- Dave Slate
dslate
 
Posts: 1
Joined: 09 May 2013, 18:14

Re: Bug in tryParse? (stores value even if parse error)

Postby guenter » 10 May 2013, 20:24

It's a (documentation) bug. The documentation should be updated to say that in case tryParse() fails, the resulting value is undefined. I've created an issue on GitHub.
guenter
 
Posts: 1092
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests

cron