Commit Graph

53 Commits

Author SHA1 Message Date
Martin Sebor 8550e48ac4 PR tree-optimization/80497 - ICE at -O1 and above on valid code on x86_64-linux-gnu in tree_to_uhwi
gcc/ChangeLog:

	PR tree-optimization/80497
	* gimple-ssa-sprintf.c (get_int_range): Avoid assuming all integer
	constants are representable in HOST_WIDE_INT.
	(parse_directive): Ditto.

gcc/testsuite/ChangeLog:

	PR tree-optimization/80497
	* gcc.dg/tree-ssa/builtin-sprintf-warn-17.c: New test.

From-SVN: r247264
2017-04-25 11:58:32 -06:00
Martin Sebor 3812f46046 PR middle-end/80364 - sanitizer detects signed integer overflow in gimple-ssa-sprintf.c
gcc/ChangeLog:
	PR middle-end/80364
	* gimple-ssa-sprintf.c (get_int_range): Remove second argument and
	always use the int type.  Use INTEGRAL_TYPE_P() rather than testing
	for INTEGER_TYPE.
	(directive::set_width, directive::set_precision, format_character):
	Adjust.
	(parse_directive): Use INTEGRAL_TYPE_P() rather than testing for
	INTEGER_TYPE.

gcc/testsuite/ChangeLog:
	PR middle-end/80364
	* gcc.dg/tree-ssa/builtin-sprintf-warn-16.c: New test.

From-SVN: r246846
2017-04-11 10:35:34 -06:00
Martin Sebor a983542b60 re PR tree-optimization/79800 (wrong snprintf result range with precision in a narrow negative-positive range)
PR tree-optimization/79800
	* gimple-ssa-sprintf.c (format_floating: Add argument.  Handle
	precision in negative-positive range.
	(format_floating): Call non-const overload with adjusted precision.

	PR tree-optimization/79800
	* gcc.dg/tree-ssa/builtin-sprintf-warn-15.c: Add test cases.
	* gcc.dg/tree-ssa/pr79800.c: New test.

From-SVN: r246151
2017-03-14 22:31:27 -06:00
Martin Sebor 71dedb336f PR middle-end/79692 - [7 Regression] -Wformat-overflow false positive
gcc/ChangeLog:

	PR middle-end/79692
	* gimple-ssa-sprintf.c
	(directive::known_width_and_precision): New function.
	(format_integer): Use it.
	(get_mpfr_format_length): Consider the full range of precision
	when computing %g output with the # flag.  Set the likely byte
	count to 3 rather than 1 when precision is indeterminate.
	(format_floating): Correct the lower bound of precision.

gcc/testsuite/ChangeLog:

	PR middle-end/79692
	* gcc.dg/tree-ssa/builtin-sprintf-2.c: Add test cases.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-10.c: Correct %#g.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-15.c: New test.
	* gcc.dg/tree-ssa/builtin-snprintf-3.c: Ditto.

From-SVN: r245822
2017-03-01 16:39:59 -07:00
Jakub Jelinek 34a64e47cd re PR tree-optimization/79327 (wrong code at -O2 and -fprintf-return-value)
PR tree-optimization/79327
	* gimple-ssa-sprintf.c (format_integer): Remove likely_adjust
	variable, its initialization and use.

From-SVN: r245542
2017-02-17 19:31:01 +01:00
Martin Sebor bf00c9e080 PR middle-end/79448 - unhelpful -Wformat-truncation=2 warning
gcc/testsuite/ChangeLog:

	PR middle-end/79448
	* gcc.dg/tree-ssa/builtin-snprintf-warn-3.c: New test.
	* gcc.dg/tree-ssa/pr79448-2.c: New test.
	* gcc.dg/tree-ssa/pr79448.c: New test.

gcc/ChangeLog:

	PR middle-end/79448
	* gimple-ssa-sprintf.c (format_directive): Avoid issuing INT_MAX
	  warning for strings of unknown length.

From-SVN: r245437
2017-02-14 09:51:24 -07:00
Martin Sebor ee75687bc6 PR middle-end/79496 - call to snprintf with zero size eliminated with -Wformat-truncation=2
gcc/ChangeLog:

	PR middle-end/79496
	* gimple-ssa-sprintf.c (pass_sprintf_length::handle_gimple_call): Avoid
	clearing info.nowrite flag when snprintf size argument is a range.

gcc/testsuite/ChangeLog:

	PR middle-end/79496
	* gcc.dg/tree-ssa/builtin-snprintf-2.c: New test.

From-SVN: r245415
2017-02-13 21:38:54 -07:00
Martin Sebor f589a1ce25 PR tree-optimization/79327 - wrong code at -O2 and -fprintf-return-value
PR tree-optimization/79327 - wrong code at -O2 and -fprintf-return-value
gcc/ChangeLog:
	* gimple-ssa-sprintf.c (tree_digits): Avoid adding the base prefix
	when precision has resulted in leading zeros.
	(format_integer): Adjust the likely counter to assume an unknown
	argument that may be zero is non-zero.

gcc/testsuite/ChangeLog:
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c: Adjust.
	* gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-13.c: New test.
	* gcc/testsuite/gcc.dg/tree-ssa/pr79327-2.c: Ditto.

From-SVN: r245173
2017-02-03 15:47:35 -07:00
Jakub Jelinek 5b00f9d236 re PR tree-optimization/79327 (wrong code at -O2 and -fprintf-return-value)
PR tree-optimization/79327
	* gimple-ssa-sprintf.c (adjust_range_for_overflow): If returning
	true, always set *argmin and *argmax to TYPE_{MIN,MAX}_VALUE of
	dirtype.
	(format_integer): Use wide_int_to_tree instead of build_int_cst
	+ to_?hwi.  If argmin is NULL, just set argmin and argmax to
	TYPE_{MIN,MAX}_VALUE of argtype.  Simplify and fix computation
	of shortest and longest sequence.

	* gcc.dg/tree-ssa/pr79327.c: New test.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c
	(test_sprintf_chk_hh_nonconst): Don't expect 2 bogus warnings.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-3.c
	(test_sprintf_chk_range_schar): Adjust dg-message.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-12.c: New test.
	* gcc.c-torture/execute/pr79327.c: New test.

Co-Authored-By: Martin Sebor <msebor@redhat.com>

From-SVN: r245166
2017-02-03 20:16:39 +01:00
Martin Sebor 3f3430400b PR tree-optimization/79352 - -fprintf-return-value doesn't handle flexible-like array members properly
gcc/ChangeLog:

	PR tree-optimization/79352
	* gimple-fold.c (get_range_strlen): Add argument.
	(get_range_strlen): Change return type to bool.
	(get_maxval_strlen): Pass in a dummy argument.
	* gimple-fold.h (get_range_strlen): Change return type to bool.
	* gimple-ssa-sprintf.c (get_string_length): Set unlikely counter.
	* tree.h (array_at_struct_end_p): Add argument.
	* tree.c (array_at_struct_end_p): Handle it.

gcc/testsuite/ChangeLog:

	PR tree-optimization/79352
	* gcc.dg/tree-ssa/pr79352.c: New test.

From-SVN: r245156
2017-02-03 09:38:15 -07:00
Martin Liska 0a95c7e2c9 Fix memory leaks in gimple-ssa-sprintf.c (PR tree-optimization/79339).
2017-02-03  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/79339
	* gimple-ssa-sprintf.c (format_floating_max): Call mpfr_clear.
	(format_floating): Likewise.

From-SVN: r245146
2017-02-03 08:20:57 +00:00
Martin Sebor ebee1eb978 PR middle-end/79275 - -Wformat-overflow false positive exceeding INT_MAX in glibc sysdeps/posix/tempname.c
gcc/testsuite/ChangeLog:

	PR middle-end/79275
	* gcc.dg/tree-ssa/builtin-sprintf-warn-11.c: New test.
	* gcc.dg/tree-ssa/pr79275.c: New test.

gcc/ChangeLog:

	PR middle-end/79275
	* gimple-ssa-sprintf.c (get_string_length): Set lower bound to zero.
	(format_string): Tighten up the range of output for non-constant
	strings and correct the expected range for wide non-constant strings.

From-SVN: r245142
2017-02-02 19:18:59 -07:00
Martin Sebor 8657c838e2 gimple-ssa-sprintf.c (fmtresult::adjust_for_width_or_precision): Move constant to the right of a relational operator.
gcc/ChangeLog:
	* gimple-ssa-sprintf.c (fmtresult::adjust_for_width_or_precision):
	Move constant to the right of a relational operator.
	(get_mpfr_format_length, format_character, format_string): Ditto.
	(should_warn_p, maybe_warn): Same.

From-SVN: r245040
2017-01-30 16:15:40 -07:00
Jakub Jelinek 89dc4c9488 realmpfr.h: Poison MPFR_RND{N,Z,U,D}.
* realmpfr.h: Poison MPFR_RND{N,Z,U,D}.
	* gimple-ssa-sprintf.c (format_floating_max): Use GMP_RNDN
	instead of MPFR_RNDN.

From-SVN: r244966
2017-01-27 12:28:34 +01:00
Martin Sebor 0fb9ec83c3 gimple-ssa-sprintf.c (format_floating): Simplify the computation of precision to avoid preprocessor conditional.
gcc/ChangeLog:
	* gimple-ssa-sprintf.c (format_floating): Simplify the computation
	of precision to avoid preprocessor conditional.

From-SVN: r244961
2017-01-26 19:45:32 -07:00
Martin Sebor 9976a81057 gimple-ssa-sprintf.c (format_floating): Test HAVE_XFmode and HAVE_DFmode before using XFmode or DFmode.
gcc/ChangeLog:
	* gimple-ssa-sprintf.c (format_floating): Test HAVE_XFmode and
	HAVE_DFmode before using XFmode or DFmode.
	(parse_directive): Avoid using the z length modifier to avoid
	the ISO C++98 does not support the ‘z’ gnu_printf length modifier.

From-SVN: r244957
2017-01-26 16:37:17 -07:00
Martin Sebor 31c87a433c PR middle-end/78703 - fprintf-return-value floating point handling incorrect in locales with a mulltibyte decimal point
gcc/ChangeLog:
        PR middle-end/78703
        * gimple-ssa-sprintf.c (adjust_for_width_or_precision): Change
        to accept adjustment as an array.
        (get_int_range): New function.
        (struct directive): Make width and prec arrays.
        (directive::set_width, directive::set_precision): Call get_int_range.
        (format_integer, format_floating): Handle width and precision ranges.
        (format_string, parse_directive): Same.

gcc/testsuite/ChangeLog:
        PR middle-end/78703
        * gcc.dg/tree-ssa/builtin-snprintf-warn-1.c: Update
        * gcc.dg/tree-ssa/builtin-sprintf-warn-9.c: Rename...
        * gcc.dg/tree-ssa/builtin-sprintf-warn-10.c: ...to this.
        * gcc.dg/tree-ssa/builtin-sprintf-warn-9.c: New test.

From-SVN: r244956
2017-01-26 16:07:02 -07:00
Martin Sebor 5d93da1d11 PR middle-end/78703 -fprintf-return-value floating point handling incorrect in locales with a mulltibyte decimal point
gcc/ChangeLog:
	PR middle-end/78703
	* gimple-ssa-sprintf.c (struct result_range): Add likely and
	unlikely counters.
	(struct format_result): Replace number_chars, number_chars_min,
	and number_chars_max with a single member of struct result_range.
	Remove bounded.
	(format_result::operator+=): Adjust.
	(struct fmtresult): Remove bounded.  Handle likely and unlikely
	counters.
	(fmtresult::adjust_for_width_or_precision): New function.
	(fmtresult:type_max_digits): New function.
	(bytes_remaining): Handle likely and unlikely counters.
	(min_bytes_remaining): Remove.
	(format_percent): Simplify.
	(format_integer, format_floating): Set likely and unlikely counters.
	(get_string_length, format_character, format_string): Same.
	(format_plain, should_warn_p): New function.
	(maybe_warn): Call should_warn_p.  Update diagnostic messages
	and handle those for all directives, including plain strings.
	(format_directive): Handle likely and unlikely counters.
	Remove unnecessary quoting from diagnostics.  Add an informational
	note.
	(add_bytes): Remove.
	(pass_sprintf_length::compute_format_length): Simplify.
	(try_substitute_return_value): Handle likely and unlikely counters.

gcc/testsuite/ChangeLog:

	PR middle-end/78703
	* gcc.dg/format/pr78569.c: Adjust.
	* gcc.dg/tree-ssa/builtin-snprintf-warn-2.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-2.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-5.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-2.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-3.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-4.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-6.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-7.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-9.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf.c: Same.

From-SVN: r244953
2017-01-26 14:14:18 -07:00
Markus Trippelsdorf cf8e20bfd8 Fix build failure with MPFR 2.4.x
* gimple-ssa-sprintf.c (format_floating): Change MPFR_RNDx to
	GMP_RNDx for compatiblity.

From-SVN: r244858
2017-01-24 09:32:16 +00:00
Martin Sebor 02f1ef408f PR middle-end/78703 - -fprintf-return-value floating point handling incorrect...
PR middle-end/78703 - -fprintf-return-value floating point handling incorrect in locales with a mulltibyte decimal point
	* gimple-ssa-sprintf.c (warn_level): New global.
	(format_integer): Use it here and throughout the rest of the file.
	Use the same switch to compute sign as base.
	(maybe_warn): New function.
	(format_directive): Factor out warnings into maybe_warn.
	Add debugging output.  Use warn_level.
	(add_bytes): Use warn_level.
	(pass_sprintf_length::compute_format_length): Add debugging output.
	(try_substitute_return_value): Same.
	(pass_sprintf_length::handle_gimple_call): Set and use warn_level.

From-SVN: r244847
2017-01-23 18:10:10 -07:00
Martin Sebor a151e93b2b PR middle-end/78703 - -fprintf-return-value floating point handling incorrect...
PR middle-end/78703 - -fprintf-return-value floating point handling incorrect in locales with a mulltibyte decimal point
	* gimple-ssa-sprintf.c (struct format_result): Remove constant member.
	(struct fmtresult, format_integer, format_floating): Adjust.
	(get_string_length, format_string,format_directive): Same.
	(pass_sprintf_length::compute_format_length): Same.
	(try_substitute_return_value): Simplify slightly.

From-SVN: r244846
2017-01-23 18:06:34 -07:00
Martin Sebor 954b452ada PR middle-end/78703 - -fprintf-return-value floating point handling incorrect...
PR middle-end/78703 - -fprintf-return-value floating point handling incorrect in locales with a mulltibyte decimal point
	* gimple-ssa-sprintf.c (pass_sprintf_length::gate): Adjust formatting.
	(fmtresult::operator+=): Outlined.
	(struct fmtresult): Add ctors.
	(struct conversion_spec): Rename...
	(struct directive): ...to this.  Add and remove data members.
	(directive::set_width, directive::set_precision): New functions.
	(format_percent): Use fmtresult ctor.
	(get_width_and_precision): Remove.
	(format_integer): Make naming changes.  Avoid computing width and
	precision.
	(format_floating): Same.  Adjust indentation.
	(format_character, format_none): New functions.
	(format_string): Moved character handling to format_character.
	(format_directive): Remove arguments, change return type.
	(parse_directive): New function.
	(pass_sprintf_length::compute_format_length): Move directive
	parsing to parse_directive.

From-SVN: r244845
2017-01-23 17:55:19 -07:00
Martin Sebor 209042e613 PR tree-optimization/78608 - gimple-ssa-sprintf.c:570:17: runtime error: negation of -9223372036854775808 cannot be represented in type 'long int'
gcc/ChangeLog:
	* gimple-ssa-sprintf.c (tree_digits): Avoid negating TYPE_MIN.

From-SVN: r244511
2017-01-16 17:14:52 -07:00
Martin Sebor 1a41367709 gimple-ssa-sprintf.c (format_directive): Correct a typo in a warning message.
gcc/ChangeLog:
	* gimple-ssa-sprintf.c (format_directive): Correct a typo in a warning
	message.

From-SVN: r244500
2017-01-16 10:08:42 -07:00
Martin Sebor bce6f76013 (-Wformat-overflow): ...to this.
gcc/c-family/ChangeLog:

	(-Wformat-overflow): ...to this.

gcc/ChangeLog:

	to -Wformat-overflow.
	* gimple-ssa-sprintf.c (pass_sprintf_length::gate): Adjust.
	(min_bytes_remaining): Same.
	(get_string_length): Same.
	(format_string): Same.
	(format_directive): Same.
	(add_bytes): Same.
	(pass_sprintf_length::handle_gimple_call): Same.

gcc/testsuite/ChangeLog:

	* gcc.dg/pr78138.c: Adjust.
	* gcc.dg/pr78768.c: Adjust.
	* gcc.dg/tree-ssa/builtin-sprintf-4.c: Adjust.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c: Adjust.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-2.c: Adjust.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-3.c: Adjust.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-4.c: Adjust.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-6.c: Adjust.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-7.c: Adjust.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-8.c: Adjust.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-9.c: Adjust.
	* gcc.dg/tree-ssa/pr78605.c: Adjust.
	* gcc.dg/tree-ssa/pr78622.c: Adjust.

From-SVN: r244385
2017-01-12 14:54:41 -07:00
Jakub Jelinek 3eefa646f4 gimple-ssa-sprintf.c (try_substitute_return_value): Remove info.nowrite calls with no lhs that can't throw.
* gimple-ssa-sprintf.c (try_substitute_return_value): Remove
	info.nowrite calls with no lhs that can't throw.  Return bool
	whether gsi_remove has been called or not.
	(pass_sprintf_length::handle_gimple_call): Return bool whether
	try_substitute_return_value called gsi_remove.  Formatting fix.
	(pass_sprintf_length::execute): Don't use gsi_remove if
	handle_gimple_call returned true.

	* gcc.dg/tree-ssa/builtin-snprintf-1.c: New test.

From-SVN: r244384
2017-01-12 22:30:56 +01:00
Martin Sebor eb07c7cffb PR middle-end/78245 - missing -Wformat-length on an overflow of a dynamically allocated buffer
gcc/testsuite/ChangeLog:

	PR middle-end/78245
	* gcc.dg/tree-ssa/builtin-sprintf-warn-3.c: Add tests.

gcc/ChangeLog:

	PR middle-end/78245
	* gimple-ssa-sprintf.c (get_destination_size): Call
	{init,fini}object_sizes.
	* tree-object-size.c (addr_object_size): Adjust.
	(pass_through_call): Adjust.
	(pass_object_sizes::execute): Adjust.
	* tree-object-size.h (fini_object_sizes): Declare.

From-SVN: r244293
2017-01-10 14:54:15 -07:00
Martin Sebor efcc8d387f PR tree-optimization/78913 - Probably misleading error reported by -Wformat-length
PR tree-optimization/78913 - Probably misleading error reported by -Wformat-length
PR middle-end/77708 - -Wformat-length %s warns for snprintf

gcc/ChangeLog:

	PR middle-end/77708
	* doc/invoke.texi (Warning Options): Document -Wformat-truncation.
	* gimple-ssa-sprintf.c (call_info::reval_used, call_info::warnopt):
	New member functions.
	(format_directive): Used them.
	(add_bytes): Same.
	(pass_sprintf_length::handle_gimple_call): Same.
	* graphite-sese-to-poly.c (tree_int_to_gmp): Increase buffer size
	to avoid truncation for any argument.
	(extract_affine_mul): Same.
	* tree.c (get_file_function_name): Same.

gcc/c-family/ChangeLog:

	PR middle-end/77708
	* c.opt (-Wformat-truncation): New option.

gcc/fortran/ChangeLog:

	PR tree-optimization/78913
	PR middle-end/77708
	* trans-common.c (build_equiv_decl): Increase buffer size to avoid
	truncation for any argument.
	* trans-types.c (gfc_build_logical_type): Same.

gcc/testsuite/ChangeLog:

	PR middle-end/77708
	* gcc.dg/tree-ssa/builtin-snprintf-warn-1.c: New test.
	* gcc.dg/tree-ssa/builtin-snprintf-warn-2.c: New test.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-6.c: XFAIL test cases failing
	due to bug 78969.
	* gcc.dg/format/pr78569.c: Adjust.

From-SVN: r244210
2017-01-08 16:42:09 -07:00
Martin Sebor 5b8999d67e PR tree-optimization/78910 - Wrong print-return-value for a negative number
gcc/ChangeLog:

	PR tree-optimization/78910
	* gimple-ssa-sprintf.c (tree_digits): Add an argument.
	(format_integer): Correct off-by-one error in the handling
	of precision with negative numbers in signed conversions..

gcc/testsuite/ChangeLog:

	PR tree-optimization/78910
	* gcc.dg/tree-ssa/builtin-sprintf-warn-7.c: Adjust text of expected
	diagnostics.
	* gcc.dg/tree-ssa/builtin-sprintf.c: Add test cases.
	* gcc.dg/tree-ssa/pr78910.c: New test.

From-SVN: r244116
2017-01-05 15:32:09 -07:00
Martin Sebor 5b4f088d0d PR tree-optimization/78696 - [7 Regression] -fprintf-return-value misoptimizes %.Ng where N is greater than 10
gcc/ChangeLog:

	PR tree-optimization/78696
	* gimple-ssa-sprintf.c (format_floating): Correct handling of
	precision.  Use MPFR for %f for greater fidelity.  Correct handling
	of %g.
	(pass_sprintf_length::compute_format_length): Set width and precision
	specified by asrerisk to void_node for vararg functions.
	(try_substitute_return_value): Adjust dump output.

gcc/testsuite/ChangeLog:

	PR tree-optimization/78696
	* gcc.dg/tree-ssa/builtin-sprintf-5.c: Remove incorrect test cases.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-7.c: Correct off-by-1 errors.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-9.c: New test.
	* gcc.dg/tree-ssa/builtin-sprintf.c: Add test cases.

From-SVN: r244037
2017-01-03 16:14:44 -07:00
Jakub Jelinek 51ad5d5ba3 re PR tree-optimization/78965 (Invalid -fprintf-return-value optimization)
PR tree-optimization/78965
	* gimple-ssa-sprintf.c (pass_sprintf_length::compute_format_length):
	Change first argument from const call_info & to call_info &.  For %n
	set info.nowrite to false.

	* gcc.dg/pr78965.c: New test.

From-SVN: r244014
2017-01-03 08:23:11 +01:00
Jakub Jelinek cd123354c5 re PR middle-end/78901 (ICE: verify_gimple failed (error: statement marked for throw in middle of block))
PR middle-end/78901
	* gimple-ssa-sprintf.c (try_substitute_return_value): Don't change
	possibly throwing calls.

	* g++.dg/opt/pr78901.C: New test.

From-SVN: r244013
2017-01-03 08:20:04 +01:00
Jakub Jelinek cbe34bb5ed Update copyright years.
From-SVN: r243994
2017-01-01 13:07:43 +01:00
Martin Sebor 2c8c83ac77 PR middle-end/78519 - missing warning for sprintf %s with null pointer
gcc/ChangeLog:

	PR middle-end/78519
	* gimple-ssa-sprintf.c (format_string): Handle null pointers.
	(format_directive): Diagnose null pointer arguments.
	(pass_sprintf_length::handle_gimple_call): Diagnose null destination
	pointers.  Correct location of null format string in diagnostics.

gcc/testsuite/ChangeLog:

	PR middle-end/78519
	* gcc.dg/tree-ssa/builtin-sprintf-warn-7.c: New test.

From-SVN: r243684
2016-12-14 21:35:31 -07:00
Martin Sebor cfce1a4a42 PR middle-end/78786 - GCC hangs/out of memory calling sprintf with large precision
gcc/ChangeLog:

	PR middle-end/78786
	* gimple-ssa-sprintf.c (target_dir_max): New macro.
	(get_mpfr_format_length): New function.
	(format_integer): Use HOST_WIDE_INT instead of int.
	(format_floating_max): Same.
	(format_floating): Call get_mpfr_format_length.
	(format_directive): Use target_dir_max.

gcc/testsuite/ChangeLog:

	PR middle-end/78786
	* gcc.dg/tree-ssa/builtin-sprintf-warn-7.c: New test.

From-SVN: r243672
2016-12-14 14:58:19 -07:00
Martin Sebor 573aa7d4b7 PR middle-end/78622 - -Wformat-length/-fprintf-return-value incorrect with overflow/wrapping
gcc/ChangeLog:

	PR middle-end/78622
	PR middle-end78606
	* gimple-ssa-sprintf.c (min_bytes_remaining): Use res.knownrange
	rather than res.bounded.
	(get_width_and_precision): Set precision to -1 when negative.
	(adjust_range_for_overflow): New function.
	(format_integer): Correct the handling of the space, plus, and pound
	flags, and the special case of zero precision.
	Always set res.bounded to true unless either precision or width
	is specified and unknown.
	Call adjust_range_for_overflow.
	Avoid use zero as the shortest value when precision is specified
	but unknown.
	(format_directive): Remove vestigial quoting.  Always inform of
	argument value or range when it's available.
	(add_bytes): Correct the computation of boundrange used to
	decide whether a warning is of a "maybe" or "defnitely" kind.

gcc/testsuite/ChangeLog:

	PR middle-end/78622
	PR middle-end78606
	* gcc.c-torture/execute/pr78622.c: New test.
	* gcc.dg/tree-ssa/builtin-sprintf-2.c: Remove "benign" undefined
	behavior inadvertently introduced in a previous commit.  Tighten
	up final checking.
	* gcc.dg/tree-ssa/builtin-sprintf-5.c: Rename macros for clarity.
	Add test cases.
	* gcc.dg/tree-ssa/builtin-sprintf-6.c: Add test cases.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-2.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-3.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-5.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-6.c: Remove xfails and
	add a final optimization check.
	* gcc.dg/tree-ssa/builtin-sprintf.c: Add test cases.
	* gcc.dg/tree-ssa/pr78622.c: New test.

From-SVN: r243582
2016-12-12 14:56:22 -07:00
Martin Sebor ee92e7bafb PR c/53562 - Add -Werror= support for -D_FORTIFY_SOURCE / __builtin___memcpy_chk
PR c/53562 - Add -Werror= support for -D_FORTIFY_SOURCE / __builtin___memcpy_chk
PR middle-end/77784 - duplicate warning for snprintf when n > object size
PR middle-end/78149 - missing warning on strncpy buffer overflow due to an excessive bound
PR middle-end/78138 - missing warnings on buffer overflow with non-constant source length

gcc/c-family/ChangeLog:

	PR c/53562
	PR middle-end/77784
	PR middle-end/78149
	PR middle-end/78138
	* c.opt (-Wstringop-overflow): New option.

gcc/ChangeLog:

	PR middle-end/77784
	PR middle-end/78149
	PR middle-end/78138
	
	* builtins.c (expand_builtin_strcat, expand_builtin_strncat): New
	functions.
	(compute_dest_size, get_size_range, check_sizes, check_strncat_sizes)
	(check_memop_sizes): Same.
	(expand_builtin_memcpy): Call check memop_sizes.
	(expand_builtin_mempcpy): Same.
	(expand_builtin_memset): Same,
	(expand_builtin_bzero): Same.
	(expand_builtin_memory_chk): Call check_sizes.
	(expand_builtin_strcpy): Same.
	(expand_builtin_strncpy): Same.
	(maybe_emit_sprintf_chk_warning): Same.
	(expand_builtin): Handle strcat and strncat.
	(fini_object_sizes): Reset pointers.
	(compute_object_size): New function.
	* gimple-ssa-sprintf.c (pass_sprintf_length::handle_gimple_call):
	Avoid issuing warnings also issued during built-in expansion.
	* doc/invoke.texi (Warning Options): Document -Wstringop-overflow.

gcc/testsuite/ChangeLog:

	PR middle-end/77784
	PR middle-end/78149
	PR middle-end/78138

	* c-c++-common/Wsizeof-pointer-memaccess2.c: Adjust expected diagnostic.
	* g++.dg/ext/builtin-object-size3.C (bar): Same.
	* g++.dg/ext/strncpy-chk1.C: Same.
	* g++.dg/opt/memcpy1.C: Same.
	* g++.dg/torture/Wsizeof-pointer-memaccess1.C: Same.
	* gcc.c-torture/compile/pr55569.c: Disable -Wstringop-overflow.
	* gcc.dg/Wobjsize-1.c: Adjust expected diagnostic.
	* gcc.dg/attr-alloc_size.c: Same.
	* gcc.dg/builtin-stringop-chk-1.c: Adjust expected diagnostic.
	* gcc.dg/builtin-stringop-chk-2.c: Same.
	* gcc.dg/builtin-stringop-chk-4.c: New test.
	* gcc.dg/builtin-strncat-chk-1.c: Adjust expected diagnostic.
	* gcc.dg/memcpy-2.c: Same.
	* gcc.dg/pr40340-1.c: Same.
	* gcc.dg/pr40340-2.c (main): Same.
	* gcc.dg/pr40340-5.c (main): Same.
	* gcc.dg/torture/Wsizeof-pointer-memaccess1.c: Same.
	* gcc.dg/torture/pr71132.c: Disable -Wstringop-overflow.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c: Adjust text of expected
	warning.
	* gfortran.dg/char_length_3.f90: Prune expected warnings.
	* gfortran.dg/pr38868.f: Add expected warnings.

From-SVN: r243419
2016-12-07 17:01:33 -07:00
Jakub Jelinek 0e81719703 re PR tree-optimization/78586 (Wrong code caused by printf-return-value)
PR tree-optimization/78586
	* gimple-ssa-sprintf.c (format_integer): Don't handle NOP_EXPR,
	CONVERT_EXPR or COMPONENT_REF here.  Formatting fix.  For
	SSA_NAME_DEF_STMT with NOP_EXPR only change argtype if the rhs1's
	type is INTEGER_TYPE or POINTER_TYPE.

From-SVN: r243145
2016-12-02 00:15:57 +01:00
Jakub Jelinek 053d5e0cdd re PR tree-optimization/78586 (Wrong code caused by printf-return-value)
PR tree-optimization/78586
	* gimple-ssa-sprintf.c (format_integer): Use TYPE_MAX_VALUE or
	TYPE_MIN_VALUE or build_all_ones_cst instead of folding LSHIFT_EXPR.
	Don't build_int_cst min/max twice.  Formatting fix.

	* gcc.c-torture/execute/pr78586.c: New test.

From-SVN: r242998
2016-11-30 09:01:47 +01:00
Martin Sebor 01a4551ceb PR tree-optimization/78512 - [7 Regression] r242674 miscompiles Linux kernel
gcc/ChangeLog:

	PR tree-optimization/78512
	* config/linux.h (TARGET_PRINTF_POINTER_FORMAT): Remove.
	* config/rs6000/linux.h: Same.
	* config/rs6000/linux64.h: Same.
	* config/sol2.h: Same.
	* config/sol2.c (solaris_printf_pointer_format): Remove.
	* doc/tm.texi.in (TARGET_PRINTF_POINTER_FORMAT): Remove.
	* doc/tm.texi: Regenerate.
	* gimple-ssa-sprintf.c (format_pointer): Rempove.
	(pass_sprintf_length::compute_format_length): Return bool.
	(pass_sprintf_length::handle_gimple_call): Adjust.
	* target.def (printf_pointer_format): Remove.
	* targhooks.c (default_printf_pointer_format): Remove.
	(linux_printf_pointer_format): Same.
	* targhooks.h (default_printf_pointer_format): Remove.
	(linux_printf_pointer_format, solaris_printf_pointer_format): Same.

gcc/testsuite/ChangeLog:

	PR tree-optimization/78512
	* gcc.dg/tree-ssa/builtin-sprintf-6.c: Add test cases.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c: Remove test cases.

From-SVN: r242975
2016-11-29 14:08:02 -07:00
Martin Sebor de6aa93370 PR middle-end/78521 - [7 Regression] incorrect byte count in -Wformat-length...
PR middle-end/78521 - [7 Regression] incorrect byte count in -Wformat-length warning with non-constant width or precision
PR middle-end/78520 - missing warning for snprintf with size greater than INT_MAX

gcc/ChangeLog:

	PR middle-end/78520
	* gimple-ssa-sprintf.c (target_max_value): Remove.
	(target_int_max, target_size_max): Use TYPE_MAX_VALUE.
	(get_width_and_precision): New function.
	(format_integer, format_floating, get_string_length, format_string):
	Correct handling of width and precision with unknown value.
	(format_directive): Add warning.
	(pass_sprintf_length::compute_format_length): Allow for precision
	to consist of a sole period with no asterisk or digits after it.

gcc/testsuite/ChangeLog:

	PR middle-end/78520
	* gcc.dg/tree-ssa/builtin-sprintf-5.c: Add test cases.
	* gcc.dg/tree-ssa/builtin-sprintf-6.c: New test.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c: Add test cases.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-3.c: Add test cases.

From-SVN: r242935
2016-11-28 14:41:41 -07:00
Jakub Jelinek 42aa1173f2 gimple-ssa-sprintf.c (build_intmax_type_nodes): Look at UINTMAX_TYPE rather than SIZE_TYPE.
* gimple-ssa-sprintf.c (build_intmax_type_nodes): Look at
	UINTMAX_TYPE rather than SIZE_TYPE.  Add gcc_unreachable if
	intmax_t couldn't be determined.
	(format_integer): Make {,u}intmax_type_node no longer static,
	initialize them only when needed.  For z and t use
	signed_or_unsigned_type_for instead of assuming size_t and
	ptrdiff_t have the same precision.

From-SVN: r242911
2016-11-28 11:01:30 +01:00
Martin Sebor d7b0fcaa39 PR tree-optimization/78476 - snprintf(0, 0, ...) with known arguments not optimized away
gcc/testsuite/ChangeLog:

	PR tree-optimization/78476
	* gcc.dg/tree-ssa/builtin-sprintf-5.c: New test.

gcc/ChangeLog:

	PR tree-optimization/78476
	* gimple-ssa-sprintf.c (struct pass_sprintf_length::call_info):
	Add a member.
	(handle_gimple_call): Adjust signature.
	(try_substitute_return_value): Remove calls to bounded functions
	with zero buffer size whose result is known.
	(pass_sprintf_length::execute): Adjust call to handle_gimple_call.

From-SVN: r242854
2016-11-24 15:45:18 -07:00
Martin Sebor aecc521b46 PR middle-end/78461 - [7 Regression] ICE: in operator+=
gcc/testsuite/ChangeLog:

	PR middle-end/78461
	* gcc.dg/tree-ssa/builtin-sprintf-4.c: New test.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-2.c: Adjust warning text.

gcc/ChangeLog:

	PR middle-end/78461
	* gimple-ssa-sprintf.c (format_string): Correct the maxima and
	set the minimum number of bytes for an unknown string to zero.

From-SVN: r242769
2016-11-23 09:44:16 -07:00
Martin Sebor 1eb4547b10 PR middle-end/77735 - FAIL: gcc.dg/tree-ssa/builtin-sprintf-warn-1.c
gcc/ChangeLog:

	PR middle-end/77735
	* builtins.c (string_length): New function.
	(c_strlen): Use string_length.  Correctly handle wide strings.
	* gimple-ssa-sprintf.c (target_max_value, target_size_max): New
	functions.
	(target_int_max): Call target_max_value.
	(format_result::knownrange): New data member.
	(fmtresult::fmtresult): Define default constructor.
	(format_integer): Use it and set format_result::knownrange.
	Handle global constants.
	(format_floating_max): Add third argument.
	(format_floating): Recompute maximum value for %a for each argument.
	(get_string_length): Use fmtresult default ctor.
	(format_string): Set format_result::knownrange.
	(format_directive): Check format_result::knownrange.
	(add_bytes): Same.  Correct caret placement in diagnostics.
	(pass_sprintf_length::compute_format_length): Set
	format_result::knownrange.
	(pass_sprintf_length::handle_gimple_call): Use target_size_max.

gcc/testsuite/ChangeLog:

	PR middle-end/77735
	* gcc.dg/tree-ssa/builtin-sprintf-2.c: Add test cases.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-2.c: Same.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-3.c: Adjust/relax.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-4.c: Add test cases.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-6.c: XFAIL for LP64 only.
	* gcc.dg/tree-ssa/builtin-sprintf.c: Add test cases.

From-SVN: r241489
2016-10-24 10:53:20 -06:00
Jakub Jelinek 91eaca5e32 gimple-ssa-sprintf.c (pass_sprintf_length::gate): Use x > 0 instead of 0 < x.
* gimple-ssa-sprintf.c (pass_sprintf_length::gate): Use x > 0 instead
	of 0 < x.
	(format_floating, format_string, format_directive,
	get_destination_size, pass_sprintf_length::handle_gimple_call):
	Likewise.

From-SVN: r240600
2016-09-28 23:52:53 +02:00
Jakub Jelinek 5fe39ec007 gimple-ssa-sprintf.c: Fix comment formatting.
* gimple-ssa-sprintf.c: Fix comment formatting.
	(format_integer): Use is_gimple_assign.
	(pass_sprintf_length::handle_gimple_call): Use gimple_call_builtin_p
	and gimple_call_fndecl.  Reorder case BUILT_IN_SPRINTF_CHK.  Fix up
	BUILT_IN_SNPRINTF_CHK comment.  Replace "to to" with "to" in comment.
	(pass_sprintf_length::execute): Use is_gimple_call.

From-SVN: r240594
2016-09-28 21:47:04 +02:00
Martin Sebor 2b1b593842 PR c/77762 - Incorrect destination buffer length in -Wformat-length warning
gcc/testsuite/ChangeLog:

	PR c/77762
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c (test_vsnprintf_chk_s):
	Call __builtin___vsnprintf_chk, not __builtin___snprintf_chk.
	(test_sprintf_p_const): Adjust line numbers to avoid failures
	introduced in r240503.

gcc/ChangeLog:

	PR c/77762
	* gimple-ssa-sprintf.c (pass_sprintf_length::handle_gimple_call):
	Fix typos.

From-SVN: r240584
2016-09-28 10:24:35 -06:00
Martin Sebor 30dc6ee970 PR middle-end/77683 - ICE on %lf directive in format_floating in
gcc/testsuite/ChangeLog:

	PR middle-end/77683
	* gcc.dg/tree-ssa/builtin-sprintf-warn-1.c: Add test cases.

gcc/ChangeLog:

	PR middle-end/77683
	* gimple-ssa-sprintf.c (format_integer): Fail gracefully when
	length modifier is not expected.
	(format_floating): Ignore l length modifier and fail gracefuly
	when it isn't one of the other expected ones.

From-SVN: r240583
2016-09-28 10:13:12 -06:00
Martin Sebor ed30d2a6f4 PR target/77676 - powerpc64 and powerpc64le stage2 bootstrap fail
gcc/testsuite/ChangeLog:

	PR target/77676
	* gcc.dg/tree-ssa/builtin-sprintf-1.c: Define and use wint_t.
	* gcc.dg/tree-ssa/builtin-sprintf-2.c: Fix typo.
	* gcc.dg/tree-ssa/builtin-sprintf-3.c: New test.
	* gcc.dg/tree-ssa/builtin-sprintf-warn-5.c: New test.

gcc/ChangeLog:

	PR target/77676
	* gimple-ssa-sprintf.c (target_int_min, target_int_max): Use
	HOST_BITS_PER_WIDE_INT, make a static local variable auto.
	(target_int_min): Correct computation.
	(format_integer): Use long long as the argument for the ll length
	modifier.
	(format_floating): Use target_int_max().
	(get_string_length): Same.
	(format_string): Avoid setting the bounded flag for strings
	of unknown length.
	(try_substitute_return_value): Avoid setting range info when
	the result isn't bounded.
	* varasm.c (assemble_name): Increase buffer size.

From-SVN: r240383
2016-09-22 15:28:07 -06:00