Opened 11 years ago

Closed 10 years ago

#6118 closed Bugs (fixed)

Uuid random_generator compiles with warnings by GCC

Reported by: Vasily Sukhanov <basil@…> Owned by: Andy Tompkins
Milestone: To Be Determined Component: uuid
Version: Boost 1.47.0 Severity: Problem
Keywords: warning gcc Cc: jonathan.jones@…

Description

uuids::random_generator constructor emits warning when compiled with GCC:

g++ -O2 -Wall -Werror uuidTest.cpp

On Linux x86_64, gcc 4.5.2 output is

/home/vasily/boost_1_47_0/boost/uuid/seed_rng.hpp: In member function ‘void boost::uuids::detail::seed_rng::sha1_random_digest_()’: /home/vasily/boost_1_47_0/boost/uuid/seed_rng.hpp:152:53: error: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result

Cross gcc 4.5.3 (Linux -> Solaris) and gcc 4.6.1 (Linux -> AIX) produce

In file included from testBoostUuid.cpp:3:0: /emc/sukhav/cross/boost_1_47_0/boost/uuid/seed_rng.hpp: In constructor ‘boost::uuids::basic_random_generator<UniformRandomNumberGenerator>::basic_random_generator() [with UniformRandomNumberGenerator = boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>]’: /emc/sukhav/cross/boost_1_47_0/boost/uuid/seed_rng.hpp:243:34: error: ‘end.boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng>::m_value’ may be used uninitialized in this function [-Werror=uninitialized]

Latter warning is regression. Compilation passes with boost 1.46.1. Probably, it's caused by some changes in Random library.

Attachments (1)

uuidTest.cpp (109 bytes ) - added by Vasily Sukhanov <basil@…> 11 years ago.
test program

Download all attachments as: .zip

Change History (15)

by Vasily Sukhanov <basil@…>, 11 years ago

Attachment: uuidTest.cpp added

test program

comment:1 by Vasily Sukhanov <basil@…>, 11 years ago

Keywords: gcc added

comment:2 by anonymous, 11 years ago

What about casting to void the call to fread?

(void) fread(...)

This should suppress the warning without adding the extra cost to check the value returned by fread.

Otherwise, there is a patch that check this value here: http://boost.2283326.n4.nabble.com/PATCH-1-2-Fixed-warning-ignoring-return-value-of-size-t-fread-void-size-t-size-t-FILE-td3145963.html with the disadvantage of adding another warning when suppressing the BOOST_ASSERT.

comment:3 by Andy Tompkins, 11 years ago

Please allow me to rephrase.

As it is, one gets the warning that the return value of std::fread is ignored.

If it is changed to:

size_t not_used = std::fread( ... );


then one gets the warning that not_used is an unused variable

but the following will have no warnings?:

(void) fread( ... );

I checked this into trunk (commit 76007). Please let me know if this does indeed suppress all warnings.

comment:4 by Vasily Sukhanov <basil@…>, 11 years ago

Doesn't solve problem. GCC 4.5.2 reports

cc1plus: warnings being treated as errors In file included from /home/vasily/boost_1_47_0/boost/uuid/random_generator.hpp:12:0,

from uuidTest.cpp:1:

/home/vasily/boost_1_47_0/boost/uuid/seed_rng.hpp: In member function ‘void boost::uuids::detail::seed_rng::sha1_random_digest_()’: /home/vasily/boost_1_47_0/boost/uuid/seed_rng.hpp:152:51: error: value computed is not used /home/vasily/boost_1_47_0/boost/uuid/seed_rng.hpp:152:51: error: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result

I am concerned more about second warning on Solaris and AIX.

comment:5 by Andy Tompkins, 11 years ago

This random number generator is slow. That is why it is only used to seed a faster random number generator. Therefore I don't believe that the cost of an extra assignment or two will be significant. I checked in the following (trunk commit 76076):

size_t not_used = 0;
not_used = fread( ... );

Does this get rid of all the warnings?

comment:6 by Vasily Sukhanov <basil@…>, 11 years ago

Compilation passes with gcc 4.5, but fails in 4.6

/emc/sukhav/cross/boost_1_47_0/boost/uuid/seed_rng.hpp:152:15: error: variable ‘not_used’ set but not used [-Werror=unused-but-set-variable]

-Wunused-but-set-variable is new warning introduced in 4.6 branch.

comment:7 by Andy Tompkins, 11 years ago

Changed

not_used = fread( ... );

to

not_used += fread( ... );

Does this get rid of all the warnings?

comment:8 by Andy Tompkins, 11 years ago

Forgot to add that I checked this into trunk revision 77049.

comment:9 by Vasily Sukhanov <basil@…>, 11 years ago

Looks like you fixed warning on Linux. But you haven't proposed a fix for the second one (AIX/Solaris). At boost/uuid/seed_rng.hpp:244 there is default-initialized generator_iterator. But generator_iterator::generator_iterator() leaves generator_iterator::m_value uninitialized. I reproduced with boost 1.48.

comment:10 by markus@…, 11 years ago

Happy it's fixed, but I think a clearer fix would be:

inline void ignore_size(size_t s){} ... ignore_size(std::fread(...));

This won't ever generate instructions or be 'outed' by a compiler as a null operation.

comment:11 by Jonathan Jones <jonathan.jones@…>, 10 years ago

Cc: jonathan.jones@… added

comment:12 by benjamin.mahler@…, 10 years ago

Any update on fixing the second warning?

end.boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng>::m_value' is used uninitialized in this function

comment:13 by Andy Tompkins, 10 years ago

I've checked in the ignore_size suggestion in the hopes that it will get rid of all warnings.

I've also checked a fix for the second warning.

Both rev 80501

comment:14 by Andy Tompkins, 10 years ago

Resolution: fixed
Status: newclosed

checked in release - changeset #80846

Note: See TracTickets for help on using tickets.