Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handy.h:1922 shift-count-overflow warning under GCC 5.2, seems impossible #14898

Closed
p5pRT opened this issue Sep 11, 2015 · 7 comments
Closed

Comments

@p5pRT
Copy link

p5pRT commented Sep 11, 2015

Migrated from rt.perl.org#126038 (status was 'resolved')

Searchable as RT126038$

@p5pRT
Copy link
Author

p5pRT commented Sep 11, 2015

From @dcollinsn

Greetings,

handy.h​:1922 defines the following macro used to determine whether or not we need to check for an overflow condition at run-time​:

/* This expression will be constant-folded at compile time. It checks
* whether or not the type of the count n is so small (e.g. U8 or U16, or
* U32 on 64-bit systems) that there's no way a wrap-around could occur.
* As well as avoiding the need for a run-time check in some cases, it's
* designed to avoid compiler warnings like​:
* comparison is always false due to limited range of data type
*/

# define _MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) \
  (sizeof(t) > (((MEM_SIZE)1) << 8*(sizeof(MEM_SIZE) - sizeof(n))))

regcomp.c at 8731, 8734, 8970, and 8973 calls macro Copy(*, *, copy_count, UV), which expands to, in part​:

_MEM_WRAP_NEEDS_RUNTIME_CHECK(copy_count,UV)

This causes the following compiler warning in GCC 5.2 when building with -Duse64bitint on x86​:

In file included from regcomp.c​:75​:0​:
regcomp.c​: In function ‘Perl__invlist_union_maybe_complement_2nd’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1947​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1947​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1954​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK’
#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
  ^
handy.h​:2065​:24​: note​: in expansion of macro ‘MEM_WRAP_CHECK_’
#define Copy(s,d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
  ^
regcomp.c​:8731​:6​: note​: in expansion of macro ‘Copy’
  Copy(array_a + i_a, array_u + i_u, copy_count, UV);
  ^

It is difficult to imagine any universe in which "left shift count >= width of type" here​:

((MEM_SIZE)1) << 8*(sizeof(MEM_SIZE) - sizeof(n)))

As the left shift count is equal to the width of MEM_SIZE minus the width of copy_count, so unless sizeof(copy_count) == 0, this warning seems outright false. Hopefully someone else can get to the bottom of this and decide whether it's an upstream or not.

@p5pRT
Copy link
Author

p5pRT commented Sep 11, 2015

From @dcollinsn

This same warning appears in pp_hot.c​:3581​:

In file included from pp_hot.c​:36​:0​:
pp_hot.c​: In function ‘Perl_pp_aelem’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1951​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1951​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
pp_hot.c​:3581​:8​: note​: in expansion of macro ‘MEM_WRAP_CHECK_1’
  MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend);
  ^

Several times in sv.c...​:

sv.c​: In function ‘Perl_sv_vcatpvfn_flags’​:
sv.c​:11447​:30​: warning​: comparison is always false due to limited range of data type [-Wtype-limits]
  if ((IV)elen < 0) {
  ^
In file included from sv.c​:32​:0​:
sv.c​: In function ‘Perl_ptr_table_new’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1947​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1947​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1954​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK’
#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
  ^
handy.h​:2043​:28​: note​: in expansion of macro ‘MEM_WRAP_CHECK_’
#define Newxz(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_ALLOC(n,t,safecalloc((n),sizeof(t)))))
  ^
sv.c​:13178​:5​: note​: in expansion of macro ‘Newxz’
  Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
  ^
sv.c​: In function ‘Perl_ptr_table_split’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1947​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1947​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1954​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK’
#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
  ^
handy.h​:2053​:10​: note​: in expansion of macro ‘MEM_WRAP_CHECK_’
  (v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_REALLOC(n,t,v,saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
  ^
sv.c​:13266​:5​: note​: in expansion of macro ‘Renew’
  Renew(ary, newsize, PTR_TBL_ENT_t*);
  ^
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1947​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1947​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1954​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK’
#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
  ^
handy.h​:2066​:22​: note​: in expansion of macro ‘MEM_WRAP_CHECK_’
#define Zero(d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memzero((char*)(d), (n) * sizeof(t)))
  ^
sv.c​:13267​:5​: note​: in expansion of macro ‘Zero’
  Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
  ^
sv.c​: In function ‘Perl_ptr_table_clear’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1947​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1947​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1954​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK’
#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
  ^
handy.h​:2066​:22​: note​: in expansion of macro ‘MEM_WRAP_CHECK_’
#define Zero(d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memzero((char*)(d), (n) * sizeof(t)))
  ^
sv.c​:13300​:2​: note​: in expansion of macro ‘Zero’
  Zero(tbl->tbl_ary, tbl->tbl_max + 1, struct ptr_tbl_ent *);
  ^

At pp.c​:2496​:

In file included from pp.c​:28​:0​:
pp.c​: In function ‘S_scomplement’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1947​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1947​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1954​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK’
#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
  ^
handy.h​:2041​:27​: note​: in expansion of macro ‘MEM_WRAP_CHECK_’
#define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_ALLOC(n,t,safemalloc((MEM_SIZE)((n)*sizeof(t))))))
  ^
pp.c​:2496​:8​: note​: in expansion of macro ‘Newx’
  Newx(result, nchar + 1, U8);
  ^

At scope.c​:79​:

In file included from scope.c​:27​:0​:
scope.c​: In function ‘Perl_cxinc’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1947​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1947​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1954​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK’
#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
  ^
handy.h​:2077​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK_’
#define PoisonWith(d,n,t,b) (MEM_WRAP_CHECK_(n,t) (void)memset((char*)(d), (U8)(b), (n) * sizeof(t)))
  ^
handy.h​:2078​:26​: note​: in expansion of macro ‘PoisonWith’
#define PoisonNew(d,n,t) PoisonWith(d,n,t,0xAB)
  ^
scope.c​:79​:5​: note​: in expansion of macro ‘PoisonNew’
  PoisonNew(cxstack + old_max + 1, cxstack_max - old_max, PERL_CONTEXT);
  ^

In perlio.c​:

In file included from perlio.c​:43​:0​:
perlio.c​: In function ‘PerlIO_list_push’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1947​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1947​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1954​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK’
#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
  ^
handy.h​:2053​:10​: note​: in expansion of macro ‘MEM_WRAP_CHECK_’
  (v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_REALLOC(n,t,v,saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
  ^
perlio.c​:547​:6​: note​: in expansion of macro ‘Renew’
  Renew(list->array, list->len, PerlIO_pair_t);
  ^
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]
  (sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
  ^
perl.h​:3517​:62​: note​: in definition of macro ‘EXPECT’
# define EXPECT(expr,val) __builtin_expect(expr,val)
  ^
perl.h​:3522​:52​: note​: in expansion of macro ‘cBOOL’
#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
  ^
handy.h​:1947​:9​: note​: in expansion of macro ‘UNLIKELY’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1943​:9​: note​: in expansion of macro ‘_MEM_WRAP_NEEDS_RUNTIME_CHECK’
  ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
  ^
handy.h​:1947​:18​: note​: in expansion of macro ‘_MEM_WRAP_WILL_WRAP’
  (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
  ^
handy.h​:1954​:30​: note​: in expansion of macro ‘MEM_WRAP_CHECK’
#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
  ^
handy.h​:2041​:27​: note​: in expansion of macro ‘MEM_WRAP_CHECK_’
#define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_ALLOC(n,t,safemalloc((MEM_SIZE)((n)*sizeof(t))))))
  ^
perlio.c​:549​:6​: note​: in expansion of macro ‘Newx’
  Newx(list->array, list->len, PerlIO_pair_t);
  ^

And that's it.

@p5pRT
Copy link
Author

p5pRT commented Sep 14, 2015

From @iabyn

On Thu, Sep 10, 2015 at 07​:34​:30PM -0700, Dan Collins wrote​:
[snip]

This causes the following compiler warning in GCC 5.2 when building with -Duse64bitint on x86​:

In file included from regcomp.c​:75​:0​:
regcomp.c​: In function ‘Perl__invlist_union_maybe_complement_2nd’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-count-overflow]

The good news is that I have already fixed this in an in-progress private
branch where I'm working on the issues raised by your RT #125937 ticket.

--
You never really learn to swear until you learn to drive.

@p5pRT
Copy link
Author

p5pRT commented Sep 14, 2015

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Aug 6, 2016

From @khwilliamson

On Mon Sep 14 03​:53​:14 2015, davem wrote​:

On Thu, Sep 10, 2015 at 07​:34​:30PM -0700, Dan Collins wrote​:
[snip]

This causes the following compiler warning in GCC 5.2 when building
with -Duse64bitint on x86​:

In file included from regcomp.c​:75​:0​:
regcomp.c​: In function ‘Perl__invlist_union_maybe_complement_2nd’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-
count-overflow]

The good news is that I have already fixed this in an in-progress
private
branch where I'm working on the issues raised by your RT #125937
ticket.

I believe that this ticket can be closed
--
Karl Williamson

@p5pRT
Copy link
Author

p5pRT commented Aug 7, 2016

From @xsawyerx

On Sat Aug 06 14​:33​:04 2016, khw wrote​:

On Mon Sep 14 03​:53​:14 2015, davem wrote​:

On Thu, Sep 10, 2015 at 07​:34​:30PM -0700, Dan Collins wrote​:
[snip]

This causes the following compiler warning in GCC 5.2 when building
with -Duse64bitint on x86​:

In file included from regcomp.c​:75​:0​:
regcomp.c​: In function ‘Perl__invlist_union_maybe_complement_2nd’​:
handy.h​:1923​:31​: warning​: left shift count >= width of type [-Wshift-
count-overflow]

The good news is that I have already fixed this in an in-progress
private
branch where I'm working on the issues raised by your RT #125937
ticket.

I believe that this ticket can be closed

I agree.

@p5pRT
Copy link
Author

p5pRT commented Aug 7, 2016

@xsawyerx - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant