LIBC string.find() bug !?

Jul 9, 2018 at 8:39pm
Has anyone encountered the following issue using the string_object.find() method?

The method is returning a large address value, larger than even the string_object.length() returns, instead of -1 (ignoring the end-of-line existence).

I am recompiling C++ code last done so under RHEL 4u3, now in RHEL 7. compiling completed successfully, but actual execution of code resulted with unexpected results.

the code utilized the following conditional checks, but would resolve true, entering the section of code, thus causing core dumps...

if ( (pos = workData.find(token)) != string::npos ) { ... }

but I needed to add the following to correct the problem...

if ( ( (pos = workData.find(token)) != string::npos ) && (pos < workData.length()) ) { ... }

Is there any known bug for this ?

Thank you in advance for any assistance.

RobM

Jul 9, 2018 at 9:14pm
What is the type of pos?
Jul 9, 2018 at 9:23pm
rrmestl wrote:
instead of -1

It can never return -1. On error, it returns string::npos, which is a large positive integer (the largest integer of its type, actually)
Jul 9, 2018 at 9:37pm
"pos" was of type "unsigned int pos"
Jul 9, 2018 at 10:12pm
yep, that was my issue... sorry to bother all... once i used size_t to define the "pos" variables, compile warning went away, as well as program working as expected again...
thx all !!!
Topic archived. No new replies allowed.