On Sun, Jan 31, 2021 at 02:12:46PM +0100, Jakub Jelinek via Gcc-patches wrote: > On Sun, Jan 31, 2021 at 01:49:29PM +0100, Eric Botcazou wrote: > > > The important difference is for strn{,case}cmp folding, we pass that s2 > > > value as the last argument to the host functions comparing the c_getstr > > > results. If s2 fits into size_t, then my patch makes no difference, > > > but if it is larger, we know the 2 c_getstr objects need to fit into the > > > host address space, so larger s2 should just act essentially as strcmp > > > or strcasecmp; as none of those objects can occupy 100% of the address > > > space, using MIN (SIZE_MAX, s2) achieves that. > > > > But SIZE_MAX is a host value, isn't it? As a matter of fact, it breaks the > > Sure. > > > build with somewhat ancient glibcs: > > > > In file included from ../../src/gcc/fold-const-call.c:21: > > > > ../../src/gcc/fold-const-call.c: In function 'tree_node* > > fold_const_call(combined_fn, tree, tree, tree, tree)': > > > > ../../src/gcc/fold-const-call.c:1777:56: error: 'SIZE_MAX' was not declared > > in > > this scope > > > > 1777 | return build_int_cst (type, strncmp (p0, p1, MIN (s2, SIZE_MAX))); > > > > because /usr/include/stdint.h has: > > > > /* The ISO C99 standard specifies that in C++ implementations these > > macros should only be defined if explicitly requested. */ > > #if !defined __cplusplus || defined __STDC_LIMIT_MACROS > > Ugh. Anyway, we don't really need the SIZE_MAX macro, we can use > INTTYPE_MAXIMUM (size_t) or (~(size_t) 0) too instead of it.
In patch form now (for ~ we'd need to use (size_t) ~(size_t) 0 to be fully portable, I think nothing in the standard requires that size_t isn't e.g. unsigned char or unsigned short that would then promote to int. Ok for trunk? 2021-01-31 Jakub Jelinek <ja...@redhat.com> * fold-const-call.c (fold_const_call): Use INTTYPE_MAXIMUM (size_t) instead of SIZE_MAX. --- gcc/fold-const-call.c.jj 2021-01-25 10:03:30.855413068 +0100 +++ gcc/fold-const-call.c 2021-01-31 18:37:04.022539076 +0100 @@ -1777,7 +1777,9 @@ fold_const_call (combined_fn fn, tree ty && !TREE_SIDE_EFFECTS (arg1)) return build_int_cst (type, 0); else if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1))) - return build_int_cst (type, strncmp (p0, p1, MIN (s2, SIZE_MAX))); + return build_int_cst (type, + strncmp (p0, p1, + MIN (s2, INTTYPE_MAXIMUM (size_t)))); return NULL_TREE; case CFN_BUILT_IN_STRNCASECMP: @@ -1789,7 +1791,7 @@ fold_const_call (combined_fn fn, tree ty return build_int_cst (type, 0); else if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)) - && strncmp (p0, p1, MIN (s2, SIZE_MAX)) == 0) + && strncmp (p0, p1, MIN (s2, INTTYPE_MAXIMUM (size_t))) == 0) return build_int_cst (type, 0); return NULL_TREE; Jakub