ccan icon indicating copy to clipboard operation
ccan copied to clipboard

base64: fix assignment to wrong type

Open mmilata opened this issue 8 years ago • 6 comments
trafficstars

Found by gcc:

base64.c:196:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (more == -1) {
           ^

mmilata avatar May 11 '17 16:05 mmilata

On Thu, May 11, 2017 at 09:40:28AM -0700, Martin Milata wrote:

Found by gcc:

base64.c:196:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (more == -1) {
           ^

You can view, comment on, or merge this pull request online at:

So, I guess that's right in a sense, but it just exposes a bug in the called function - that returns an int value derived from a size_t, which could be truncated on a 64-bit platform. I think that needs to change to ssize_t.

Peter (module author), can you have a look at this.

https://github.com/rustyrussell/ccan/pull/56

-- Commit Summary --

  • base64: fix assignment to wrong type

-- File Changes --

M ccan/base64/base64.c (2)

-- Patch Links --

https://github.com/rustyrussell/ccan/pull/56.patch https://github.com/rustyrussell/ccan/pull/56.diff

-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT the other | way around! http://www.ozlabs.org/~dgibson

dgibson avatar May 12 '17 14:05 dgibson

From program flow maximum size of the parameter is 3, so overflow is unlikely.

Changing it to return ssize_t would seem to be reasonable, 'though.

@mmilata Do you think changing the return type to ssize_t is reasonable? If so, would you like to do a PR for that?

peterbarker avatar May 14 '17 23:05 peterbarker

@peterbarker Updated the PR to use ssize_t. I've also changed the return type of base64_decode_quartet_using_maps for the sake of consistency. Please take a look?

mmilata avatar May 15 '17 00:05 mmilata

:+1: @rustyrussell please merge, then elementsproject/libwally-core@bf81e8b will need a ccan-update (noticed today while compiling current elementsproject/lightning@9470ea303)

jsarenik avatar Apr 25 '21 18:04 jsarenik

Just that it would be nicer without the warning… =)

../../../libwally-core/src/ccan/ccan/base64/base64.c: In function 'base64_decode_using_maps':
../../../libwally-core/src/ccan/ccan/base64/base64.c:196:11: warning: comparison
 of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'}
 and 'int' [-Wsign-compare]
  196 |  if (more == -1) {
      |           ^~

jsarenik avatar Apr 25 '21 18:04 jsarenik

Thanks, applied!

rustyrussell avatar Sep 08 '21 05:09 rustyrussell