Fix #290: wchar_t may vary in size

This commit is contained in:
Arun Prakash Jana 2019-06-22 07:19:00 +05:30
parent 42a5479fa8
commit a9392463b0
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -151,8 +151,8 @@
#define F_CLI (F_NORMAL | F_MULTI) #define F_CLI (F_NORMAL | F_MULTI)
/* CRC8 macros */ /* CRC8 macros */
#define WIDTH (sizeof(unsigned char) << 3) #define UCHAR_BIT_WIDTH (sizeof(unsigned char) << 3)
#define TOPBIT (1 << (WIDTH - 1)) #define TOPBIT (1 << (UCHAR_BIT_WIDTH - 1))
#define POLYNOMIAL 0xD8 /* 11011 followed by 0's */ #define POLYNOMIAL 0xD8 /* 11011 followed by 0's */
#define CRC8_TABLE_LEN 256 #define CRC8_TABLE_LEN 256
@ -507,7 +507,7 @@ static uchar crc8fast(const uchar * const message, size_t n)
/* Divide the message by the polynomial, a byte at a time */ /* Divide the message by the polynomial, a byte at a time */
while (byte < n) { while (byte < n) {
data = message[byte] ^ (remainder >> (WIDTH - 8)); data = message[byte] ^ (remainder >> (UCHAR_BIT_WIDTH - 8));
remainder = crc8table[data] ^ (remainder << 8); remainder = crc8table[data] ^ (remainder << 8);
++byte; ++byte;
} }
@ -1766,6 +1766,7 @@ static char *xreadline(char *prefill, char *prompt)
{ {
size_t len, pos; size_t len, pos;
int x, y, r; int x, y, r;
const int WCHAR_T_WIDTH = sizeof(wchar_t);
wint_t ch[2] = {0}; wint_t ch[2] = {0};
wchar_t * const buf = malloc(sizeof(wchar_t) * CMD_LEN_MAX); wchar_t * const buf = malloc(sizeof(wchar_t) * CMD_LEN_MAX);
@ -1805,7 +1806,8 @@ static char *xreadline(char *prefill, char *prompt)
case 127: // fallthrough case 127: // fallthrough
case '\b': /* rhel25 sends '\b' for backspace */ case '\b': /* rhel25 sends '\b' for backspace */
if (pos > 0) { if (pos > 0) {
memmove(buf + pos - 1, buf + pos, (len - pos) << 2); memmove(buf + pos - 1, buf + pos,
(len - pos) * WCHAR_T_WIDTH);
--len, --pos; --len, --pos;
} // fallthrough } // fallthrough
case '\t': /* TAB breaks cursor position, ignore it */ case '\t': /* TAB breaks cursor position, ignore it */
@ -1822,7 +1824,7 @@ static char *xreadline(char *prefill, char *prompt)
continue; continue;
case CONTROL('U'): case CONTROL('U'):
printprompt(prompt); printprompt(prompt);
memmove(buf, buf + pos, (len - pos) << 2); memmove(buf, buf + pos, (len - pos) * WCHAR_T_WIDTH);
len -= pos; len -= pos;
pos = 0; pos = 0;
continue; continue;
@ -1836,7 +1838,8 @@ static char *xreadline(char *prefill, char *prompt)
continue; continue;
if (pos < CMD_LEN_MAX - 1) { if (pos < CMD_LEN_MAX - 1) {
memmove(buf + pos + 1, buf + pos, (len - pos) << 2); memmove(buf + pos + 1, buf + pos,
(len - pos) * WCHAR_T_WIDTH);
buf[pos] = *ch; buf[pos] = *ch;
++len, ++pos; ++len, ++pos;
continue; continue;
@ -1853,14 +1856,15 @@ static char *xreadline(char *prefill, char *prompt)
break; break;
case KEY_BACKSPACE: case KEY_BACKSPACE:
if (pos > 0) { if (pos > 0) {
memmove(buf + pos - 1, buf + pos, (len - pos) << 2); memmove(buf + pos - 1, buf + pos,
(len - pos) * WCHAR_T_WIDTH);
--len, --pos; --len, --pos;
} }
break; break;
case KEY_DC: case KEY_DC:
if (pos < len) { if (pos < len) {
memmove(buf + pos, buf + pos + 1, memmove(buf + pos, buf + pos + 1,
(len - pos - 1) << 2); (len - pos - 1) * WCHAR_T_WIDTH);
--len; --len;
} }
break; break;
@ -1883,7 +1887,7 @@ END:
clearprompt(); clearprompt();
buf[len] = '\0'; buf[len] = '\0';
wcstombs(g_buf, buf, ++len); wcstombs(g_buf, buf, CMD_LEN_MAX);
free(buf); free(buf);
return g_buf; return g_buf;
} }