Bug 54298 - Add warning when doing equal/nonequal floating-point comparisons
Summary: Add warning when doing equal/nonequal floating-point comparisons
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: ---
Assignee: Thomas Koenig
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2012-08-17 15:15 UTC by Tobias Burnus
Modified: 2012-08-19 16:08 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-08-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2012-08-17 15:15:25 UTC
Suggested by ISO/IEC Project 22.24772 "Guidance for Avoiding Vulnerabilities through Language Selection and Use"  (see links at http://gcc.gnu.org/wiki/GFortranStandards)

From the Fortran appendix (draft at http://grouper.ieee.org/groups/plv/DocLog/400-499/400-419/22-WG23-N-0410/n0410.pdf)


"Fortran.5 Floating-point Arithmetic [PFL]
...
"Fortran.5.2 Guidance to Fortran Users"
...
"* Do not create a logical value from a test for equality or inequality between two floating-point expressions. Use compiler options where available to detect such a usage"


gfortran doesn't have such a warning.
Comment 1 Thomas Koenig 2012-08-17 16:07:43 UTC
Sounds doable.

I'll give it a shot.
Comment 2 Tobias Burnus 2012-08-17 16:24:44 UTC
(In reply to comment #1)
> Sounds doable.
> I'll give it a shot.

Thanks. I think it can help to find some real-world issues, even if comparisons such as "a == 0.0" are generally safe.

(In reply to comment #0)
> Suggested by ISO/IEC Project 22.24772 "Guidance for Avoiding Vulnerabilities
> through Language Selection and Use"  (see links at
> http://gcc.gnu.org/wiki/GFortranStandards)
> 
> From the Fortran appendix (draft at

Wrong link (that's the current 2nd-version draft of the TR, still the Fortran part). The Fortran link is:
  ftp://ftp.nag.co.uk/sc22wg5/N1901-N1950/N1929.pdf


The issue is also mentioned in "Fortran.58 Implications for Standardization" under "Future standardization efforts should consider:"

"Requiring that processors have the ability to detect and report the occurrence within a submitted program unit of tests for equality between two objects of type real or complex."
Comment 3 Thomas Koenig 2012-08-19 15:05:49 UTC
Author: tkoenig
Date: Sun Aug 19 15:05:41 2012
New Revision: 190516

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=190516
Log:
2012-08-19  Thomas König  <tkoenig@gcc.gnu.org>

	PR fortran/54298
	* gfortran.h (struct gfc_option_t): Add warn_compare_reals.
	* lang.opt:  Add Wcompare-reals.
	* invoke.texi:  Document -Wcompare-reals.
	* resolve.c (resolve_operator):  If -Wcompare-reals is in effect,
	warn about equality/inequality comparisions for REAL and COMPLEX.
	* options.c (gfc_init_options):  Set warn_compare_reals.
	(set_Wall):  Include warn_compare_reals in Wall.
	(gfc_handle_option):  Handle Wcompare_reals.

2012-08-19  Thomas König  <tkoenig@gcc.gnu.org>

	PR fortran/54298
	* gfortran.dg/real_compare_1.f90:  New test case.
	* gfortran.dg/bessel_5.f90  Add -Wno-compare-reals to options.


Added:
    trunk/gcc/testsuite/gfortran.dg/real_compare_1.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/invoke.texi
    trunk/gcc/fortran/lang.opt
    trunk/gcc/fortran/options.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/bessel_5.f90
Comment 4 Thomas Koenig 2012-08-19 16:08:37 UTC
Fixed on trunk, closing.