basic: fix potentially invalid shifts

- use unsigned arithmetics to avoid implementation-defined behavior
  on (1 << 31)
This commit is contained in:
Charlie Gordon 2017-03-15 23:17:58 +00:00
commit 252e2848ab
3 changed files with 37 additions and 37 deletions

View file

@ -2,7 +2,7 @@
* Basic Charset functions for QEmacs
*
* Copyright (c) 2000-2002 Fabrice Bellard.
* Copyright (c) 2002-2016 Charlie Gordon.
* Copyright (c) 2002-2017 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -136,9 +136,9 @@ struct QECharset charset_raw = {
static int probe_8859_1(qe__unused__ QECharset *charset, const u8 *buf, int size)
{
static const uint32_t magic = (1 << '\b') | (1 << '\t') | (1 << '\f') |
(1 << '\n') | (1 << '\r') | (1 << '\033') |
(1 << 0x0e) | (1 << 0x0f) | (1 << 0x1f);
static const uint32_t magic = (1U << '\b') | (1U << '\t') | (1U << '\f') |
(1U << '\n') | (1U << '\r') | (1U << '\033') |
(1U << 0x0e) | (1U << 0x0f) | (1U << 0x1f);
const u8 *p = buf;
const u8 *p_end = p + size;
uint32_t c;
@ -156,7 +156,7 @@ static int probe_8859_1(qe__unused__ QECharset *charset, const u8 *buf, int size
if (c == '\n')
count_lines++;
else
if (!(magic & (1 << c)))
if (!(magic & (1U << c)))
return 0;
} else
if (c < 0x7F) {
@ -363,9 +363,9 @@ int utf8_to_unicode(unsigned int *dest, int dest_length, const char *str)
static int probe_utf8(qe__unused__ QECharset *charset, const u8 *buf, int size)
{
static const uint32_t magic = (1 << '\b') | (1 << '\t') | (1 << '\f') |
(1 << '\n') | (1 << '\r') | (1 << '\033') |
(1 << 0x0e) | (1 << 0x0f) | (1 << 0x1f);
static const uint32_t magic = (1U << '\b') | (1U << '\t') | (1U << '\f') |
(1U << '\n') | (1U << '\r') | (1U << '\033') |
(1U << 0x0e) | (1U << 0x0f) | (1U << 0x1f);
const u8 *p = buf;
const u8 *p_end = p + size;
uint32_t c;
@ -383,7 +383,7 @@ static int probe_utf8(qe__unused__ QECharset *charset, const u8 *buf, int size)
if (c == '\n')
count_lines++;
else
if (!(magic & (1 << c)))
if (!(magic & (1U << c)))
return 0;
} else
if (c < 0x7F) {
@ -567,9 +567,9 @@ struct QECharset charset_utf8 = {
static int probe_ucs2le(qe__unused__ QECharset *charset, const u8 *buf, int size)
{
static const uint32_t magic = (1 << '\b') | (1 << '\t') | (1 << '\f') |
(1 << '\n') | (1 << '\r') | (1 << '\033') |
(1 << 0x0e) | (1 << 0x0f) | (1 << 0x1f);
static const uint32_t magic = (1U << '\b') | (1U << '\t') | (1U << '\f') |
(1U << '\n') | (1U << '\r') | (1U << '\033') |
(1U << 0x0e) | (1U << 0x0f) | (1U << 0x1f);
const u8 *p = buf;
const u8 *p_end = p + (size & ~1);
uint32_t c;
@ -590,7 +590,7 @@ static int probe_ucs2le(qe__unused__ QECharset *charset, const u8 *buf, int size
if (c == '\n')
count_lines++;
else
if (!(magic & (1 << c)))
if (!(magic & (1U << c)))
return 0;
} else
if (c >= 0x10000) {
@ -703,9 +703,9 @@ static int charset_goto_line_ucs2(CharsetDecodeState *s,
static int probe_ucs2be(qe__unused__ QECharset *charset, const u8 *buf, int size)
{
static const uint32_t magic = (1 << '\b') | (1 << '\t') | (1 << '\f') |
(1 << '\n') | (1 << '\r') | (1 << '\033') |
(1 << 0x0e) | (1 << 0x0f) | (1 << 0x1f);
static const uint32_t magic = (1U << '\b') | (1U << '\t') | (1U << '\f') |
(1U << '\n') | (1U << '\r') | (1U << '\033') |
(1U << 0x0e) | (1U << 0x0f) | (1U << 0x1f);
const u8 *p = buf;
const u8 *p_end = p + (size & ~1);
uint32_t c;
@ -726,7 +726,7 @@ static int probe_ucs2be(qe__unused__ QECharset *charset, const u8 *buf, int size
if (c == '\n')
count_lines++;
else
if (!(magic & (1 << c)))
if (!(magic & (1U << c)))
return 0;
} else
if (c >= 0x10000) {
@ -847,9 +847,9 @@ struct QECharset charset_ucs2be = {
static int probe_ucs4le(qe__unused__ QECharset *charset, const u8 *buf, int size)
{
static const uint32_t magic = (1 << '\b') | (1 << '\t') | (1 << '\f') |
(1 << '\n') | (1 << '\r') | (1 << '\033') |
(1 << 0x0e) | (1 << 0x0f) | (1 << 0x1f);
static const uint32_t magic = (1U << '\b') | (1U << '\t') | (1U << '\f') |
(1U << '\n') | (1U << '\r') | (1U << '\033') |
(1U << 0x0e) | (1U << 0x0f) | (1U << 0x1f);
const u8 *p = buf;
const u8 *p_end = p + (size & ~3);
uint32_t c;
@ -870,7 +870,7 @@ static int probe_ucs4le(qe__unused__ QECharset *charset, const u8 *buf, int size
if (c == '\n')
count_lines++;
else
if (!(magic & (1 << c)))
if (!(magic & (1U << c)))
return 0;
} else
if (c > 0x10FFFF) {
@ -977,9 +977,9 @@ static int charset_goto_line_ucs4(CharsetDecodeState *s,
static int probe_ucs4be(qe__unused__ QECharset *charset, const u8 *buf, int size)
{
static const uint32_t magic = (1 << '\b') | (1 << '\t') | (1 << '\f') |
(1 << '\n') | (1 << '\r') | (1 << '\033') |
(1 << 0x0e) | (1 << 0x0f) | (1 << 0x1f);
static const uint32_t magic = (1U << '\b') | (1U << '\t') | (1U << '\f') |
(1U << '\n') | (1U << '\r') | (1U << '\033') |
(1U << 0x0e) | (1U << 0x0f) | (1U << 0x1f);
const u8 *p = buf;
const u8 *p_end = p + (size & ~3);
uint32_t c;
@ -1000,7 +1000,7 @@ static int probe_ucs4be(qe__unused__ QECharset *charset, const u8 *buf, int size
if (c == '\n')
count_lines++;
else
if (!(magic & (1 << c)))
if (!(magic & (1U << c)))
return 0;
} else
if (c > 0x10FFFF) {
@ -1509,13 +1509,13 @@ done_utf8:
has_binary = 0;
{
static const uint32_t magic = (1 << '\b') | (1 << '\t') | (1 << '\f') |
(1 << '\n') | (1 << '\r') | (1 << '\033') |
(1 << 0x0e) | (1 << 0x0f) | (1 << 0x1f);
static const uint32_t magic = (1U << '\b') | (1U << '\t') | (1U << '\f') |
(1U << '\n') | (1U << '\r') | (1U << '\033') |
(1U << 0x0e) | (1U << 0x0f) | (1U << 0x1f);
for (i = 0; i < size; i++) {
c = buf[i];
if (c < 32 && !(magic & (1 << c)))
if (c < 32 && !(magic & (1U << c)))
has_binary += 1;
}
}

10
hex.c
View file

@ -2,7 +2,7 @@
* Hexadecimal modes for QEmacs.
*
* Copyright (c) 2000-2001 Fabrice Bellard.
* Copyright (c) 2002-2016 Charlie Gordon.
* Copyright (c) 2002-2017 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -193,14 +193,14 @@ static int hex_mode_init(EditState *s, EditBuffer *b, int flags)
static int detect_binary(const unsigned char *buf, int size)
{
static const uint32_t magic = (1 << '\b') | (1 << '\t') | (1 << '\f') |
(1 << '\n') | (1 << '\r') | (1 << '\033') |
(1 << 0x0e) | (1 << 0x0f) | (1 << 0x1f);
static const uint32_t magic = (1U << '\b') | (1U << '\t') | (1U << '\f') |
(1U << '\n') | (1U << '\r') | (1U << '\033') |
(1U << 0x0e) | (1U << 0x0f) | (1U << 0x1f);
int i, c;
for (i = 0; i < size; i++) {
c = buf[i];
if (c < 32 && !(magic & (1 << c)))
if (c < 32 && !(magic & (1U << c)))
return 1;
}
return 0;

6
html.c
View file

@ -2,7 +2,7 @@
* Graphical HTML mode for QEmacs.
*
* Copyright (c) 2001-2002 Fabrice Bellard.
* Copyright (c) 2003-2016 Charlie Gordon.
* Copyright (c) 2003-2017 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -860,7 +860,7 @@ static void html_mode_free(EditBuffer *b, void *state)
/* search for HTML tag */
static int html_mode_probe(ModeDef *mode, ModeProbeData *p1)
{
static const uint32_t magic = (1 << '\r') | (1 << '\n') | (1 << '\t') | (1 << '\033');
static const uint32_t magic = (1U << '\r') | (1U << '\n') | (1U << '\t') | (1U << '\033');
const unsigned char *p = p1->buf;
int c, score;
@ -877,7 +877,7 @@ static int html_mode_probe(ModeDef *mode, ModeProbeData *p1)
c = *p;
if (c == '\0')
break;
if (c < 32 && !(magic & (1 << c)))
if (c < 32 && !(magic & (1U << c)))
return 0;
if (c == '<' && stristart(cs8(p), "<html", NULL))
score = 95;