Opened 11 years ago

Last modified 5 years ago

#5598 new Bugs

boost/property_tree/detail/json_parser_write.hpp:35: warning: comparison is always true due to limited range of data type

Reported by: benoit@… Owned by: Sebastian Redl
Milestone: To Be Determined Component: property_tree
Version: Boost 1.46.1 Severity: Problem
Keywords: Cc: mika.fischer@…, mateusz@…

Description

Here is the relevant output:

cc1plus: warnings being treated as errors
gcc41_64/include/boost/property_tree/detail/json_parser_write.hpp: In function 'std::basic_string<_CharT, std::char_traits<_CharT>, std::allocator<_CharT> > boost::property_tree::json_parser::create_escapes(const std::basic_string<_CharT, std::char_traits<_CharT>, std::allocator<_CharT> >&) [with Ch = char]':
gcc41_64/include/boost/property_tree/detail/json_parser_write.hpp:78:   instantiated from 'void boost::property_tree::json_parser::write_json_helper(std::basic_ostream<typename Ptree::key_type::value_type, std::char_traits<typename Ptree::key_type::value_type> >&, const Ptree&, int, bool) [with Ptree = boost::property_tree::basic_ptree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]'
gcc41_64/include/boost/property_tree/detail/json_parser_write.hpp:162:   instantiated from 'void boost::property_tree::json_parser::write_json_internal(std::basic_ostream<typename Ptree::key_type::value_type, std::char_traits<typename Ptree::key_type::value_type> >&, const Ptree&, const std::string&, bool) [with Ptree = boost::property_tree::basic_ptree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]'
gcc41_64/include/boost/property_tree/json_parser.hpp:98:   instantiated from 'void boost::property_tree::json_parser::write_json(std::basic_ostream<typename Ptree::key_type::value_type, std::char_traits<typename Ptree::key_type::value_type> >&, const Ptree&, bool) [with Ptree = boost::property_tree::basic_ptree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]'
json.cc:36:   instantiated from here
gcc41_64/include/boost/property_tree/detail/json_parser_write.hpp:35: warning: comparison is always true due to limited range of data type
make: *** [json.o] Error 1

In the big if() statement, *b (of type Ch, which is char) is being compared with 0xFF.

Attachments (3)

json_parser_write.hpp.diff (2.0 KB ) - added by Shane Turner <shane.turner@…> 10 years ago.
Silence the warning from GCC about a comparison that is always true due to to the limits of the type by creating specializations of the check for char and unsigned char.
json_parser_write.hpp.2.diff (2.0 KB ) - added by Matthieu Vachon <matthieu.o.vachon@…> 9 years ago.
Updated patch for 1.53.0
json_parser_write.hpp (2.1 KB ) - added by shane.turner@… 7 years ago.
Updated patch for 1.59.0

Download all attachments as: .zip

Change History (14)

comment:1 by Sebastian Redl, 11 years ago

Ch might be wchar_t.

The warning can be suppressed by casting to some large integer type first, but I feel that the warning here is overeager and a bug.

comment:2 by David Benoit <benoit@…>, 11 years ago

Sorry, I was stating that in my case, Ch was char. Yes, it could be wchar_t as well. However, in the case of Ch being char, the warning is correct.

comment:3 by anonymous, 11 years ago

Please create a comparison function for wide characters and specialize for Ch=char or unsigned char to not perform the comparison *b <= 0xff.

Thanks.

by Shane Turner <shane.turner@…>, 10 years ago

Attachment: json_parser_write.hpp.diff added

Silence the warning from GCC about a comparison that is always true due to to the limits of the type by creating specializations of the check for char and unsigned char.

comment:4 by Mika Fischer <mika.fischer@…>, 10 years ago

Cc: mika.fischer@… added

by Matthieu Vachon <matthieu.o.vachon@…>, 9 years ago

Updated patch for 1.53.0

comment:5 by Matthieu Vachon <matthieu.o.vachon@…>, 9 years ago

@Shane

I attached an updated version of the patch to this tracker.

However, the patch fails on my project because I had a double include of <boost/property_tree/json_parser.hpp> in two different compilation units. This caused my program build to fail due to a multiple definition for the check_is_ascii method. I created a simple test case that can be used to reproduce/debug the problem, I can send it to you if you want to.

If I have a time, I will check how this could be solved. For now, I just ensured that the json_parser.hpp file is included only once in my codebase.

Expect for this, your patch seems to work like a charm. Will report back if I find any problem with it.

Regards, Matt

comment:6 by Mateusz Loskot, 9 years ago

Cc: mateusz@… added

in reply to:  6 comment:7 by mk.boost@…, 9 years ago

Replying to mloskot: I think, for explicitly instantiated template functions keyword 'inline' would fix the problem

in reply to:  5 comment:8 by Shane Turner <shane.turner@…>, 9 years ago

Replying to Matthieu Vachon <matthieu.o.vachon@…>: Any chance you've tried out adding 'inline' as suggested in 7?

Also, will this change make it into 1.55.0?

comment:9 by chamara, 7 years ago

Can you confirm what version this fix is going to go in?

by shane.turner@…, 7 years ago

Attachment: json_parser_write.hpp added

Updated patch for 1.59.0

comment:10 by shane.turner@…, 7 years ago

As of Boost 1.61.0 Beta 1 RC2, the file has changed from boost/property_tree/detail/json_parser_write.hpp to boost/property_tree/json_parser/detail/write.hpp

comment:11 by anonymous, 5 years ago

In 1.64.0, the issue happens again, the fix in json_parser_write.hpp.2.diff is lost.

Note: See TracTickets for help on using tickets.