https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94815
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Resolution|--- |WONTFIX Status|NEW |RESOLVED --- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> --- The warning is only issued with optimization enabled the strlen pass disabled, such as at -O1, but not at -O2. The strlen dump below helps explain why: when the strlen pass doesn't run the warning doesn't compute the length of str2 (as reflected by the very large value in the Result of the %s directive) and so it makes a decision solely on the basis that the %s argument points to the same array as the destination of the sprintf call. This is by design. To avoid the warning under these conditions use a precision in the directive: %.3s. ;; Function f (f, funcdef_no=0, decl_uid=1938, cgraph_uid=1, symbol_order=0) ;; 1 loops found ;; ;; Loop 0 ;; header 0, latch 1 ;; depth 0, outer -1 ;; nodes: 0 1 2 ;; 2 succs { 1 } Computing maximum subobject size for buf_3: pr94815.c:13: sprintf: objsize = 9223372036854775807, fmtstr = "ABC%s" Directive 1 at offset 0: "ABC", length = 3 Result: 3, 3, 3, 3 (3, 3, 3, 3) Directive 2 at offset 3: "%s" Result: 0, 0, -1, 9223372036854775807 (3, 3, -1, -1) Directive 3 at offset 5: "", length = 1 pr94815.c: In function ‘f’: pr94815.c:13:5: warning: ‘sprintf’ argument 3 may overlap destination object ‘buf’ [-Wrestrict] 13 | sprintf(str1, "ABC%s", str2); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~