Skip to content

Commit

Permalink
Import: avoid left-shift of negative number
Browse files Browse the repository at this point in the history
This is libarchive's:

    2013-05-29
    Rework the sign-extension to avoid left-shift of an explicit negative number (which newer GCC complains about).

    libarchive/libarchive@533e8fd

Reported by:	clang scan-build
  • Loading branch information
kientzle authored and gperciva committed Jan 29, 2022
1 parent 513481f commit d9b4fc4
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions libarchive/archive_read_support_format_tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -2099,20 +2099,16 @@ tar_atol256(const char *_p, unsigned char_cnt)
upper_limit = INT64_MAX / 256;
lower_limit = INT64_MIN / 256;

/* Pad with 1 or 0 bits, depending on sign. */
/* Sign-extend the 7-bit value to 64 bits. */
if ((0x40 & *p) == 0x40)
l = (int64_t)-1;
l = ~((int64_t)0x3f) | *p++;
else
l = 0;
l = (l << 6) | (0x3f & *p++);
l = 0x3f & *p++;
while (--char_cnt > 0) {
if (l > upper_limit) {
l = INT64_MAX; /* Truncate on overflow */
break;
} else if (l < lower_limit) {
l = INT64_MIN;
break;
}
if (l > upper_limit)
return (INT64_MAX); /* Truncate on overflow */
else if (l < lower_limit)
return (INT64_MIN);
l = (l << 8) | (0xff & (int64_t)*p++);
}
return (l);
Expand Down

0 comments on commit d9b4fc4

Please sign in to comment.