aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-11-18 11:34:43 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2009-11-18 11:34:43 +0100
commit8684cbb5cc2c461e3795fba19ad7386db37cf499 (patch)
tree027d4aa55cfa710769c86c2aca838fbba3e3dbe9
parent5b0a7f1a6e66af3f1ff4159d4eb96c30517782b8 (diff)
downloadbusybox-w32-8684cbb5cc2c461e3795fba19ad7386db37cf499.tar.gz
busybox-w32-8684cbb5cc2c461e3795fba19ad7386db37cf499.tar.bz2
busybox-w32-8684cbb5cc2c461e3795fba19ad7386db37cf499.zip
libbb: robustify isXXXX(). +39 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/sort.c2
-rw-r--r--coreutils/tr.c2
-rw-r--r--editors/diff.c2
-rw-r--r--editors/vi.c4
-rw-r--r--include/libbb.h26
-rw-r--r--libbb/dump.c8
-rw-r--r--loginutils/getty.c2
-rw-r--r--miscutils/strings.c2
-rw-r--r--networking/inetd.c5
9 files changed, 28 insertions, 25 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c
index fad6d1244..e2e7983a1 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -128,7 +128,7 @@ static char *get_key(char *str, struct sort_key *key, int flags)
128 /* Handle -i */ 128 /* Handle -i */
129 if (flags & FLAG_i) { 129 if (flags & FLAG_i) {
130 for (start = end = 0; str[end]; end++) 130 for (start = end = 0; str[end]; end++)
131 if (isprint(str[end])) 131 if (isprint_asciionly(str[end]))
132 str[start++] = str[end]; 132 str[start++] = str[end];
133 str[start] = '\0'; 133 str[start] = '\0';
134 } 134 }
diff --git a/coreutils/tr.c b/coreutils/tr.c
index d89b80bec..6d4cb4a14 100644
--- a/coreutils/tr.c
+++ b/coreutils/tr.c
@@ -153,7 +153,7 @@ static unsigned expand(const char *arg, char **buffer_p)
153 } 153 }
154 if (j == CLASS_punct || j == CLASS_cntrl) { 154 if (j == CLASS_punct || j == CLASS_cntrl) {
155 for (i = '\0'; i < ASCII; i++) { 155 for (i = '\0'; i < ASCII; i++) {
156 if ((j == CLASS_punct && isprint(i) && !isalnum(i) && !isspace(i)) 156 if ((j == CLASS_punct && isprint_asciionly(i) && !isalnum(i) && !isspace(i))
157 || (j == CLASS_cntrl && iscntrl(i)) 157 || (j == CLASS_cntrl && iscntrl(i))
158 ) { 158 ) {
159 buffer[pos++] = i; 159 buffer[pos++] = i;
diff --git a/editors/diff.c b/editors/diff.c
index e977f4d14..745ef0a33 100644
--- a/editors/diff.c
+++ b/editors/diff.c
@@ -749,7 +749,7 @@ static int asciifile(FILE *f)
749 rewind(f); 749 rewind(f);
750 cnt = fread(g_read_buf, 1, COMMON_BUFSIZE, f); 750 cnt = fread(g_read_buf, 1, COMMON_BUFSIZE, f);
751 for (i = 0; i < cnt; i++) { 751 for (i = 0; i < cnt; i++) {
752 if (!isprint(g_read_buf[i]) 752 if (!isprint_asciionly(g_read_buf[i])
753 && !isspace(g_read_buf[i]) 753 && !isspace(g_read_buf[i])
754 ) { 754 ) {
755 return 0; 755 return 0;
diff --git a/editors/vi.c b/editors/vi.c
index 6a6722875..7d83db642 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -30,9 +30,9 @@
30#if ENABLE_LOCALE_SUPPORT 30#if ENABLE_LOCALE_SUPPORT
31 31
32#if ENABLE_FEATURE_VI_8BIT 32#if ENABLE_FEATURE_VI_8BIT
33#define Isprint(c) isprint(c) 33# define Isprint(c) isprint(c)
34#else 34#else
35#define Isprint(c) (isprint(c) && (unsigned char)(c) < 0x7f) 35# define Isprint(c) (isprint(c) && (unsigned char)(c) < 0x7f)
36#endif 36#endif
37 37
38#else 38#else
diff --git a/include/libbb.h b/include/libbb.h
index d95be5c51..77c9e2888 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1606,10 +1606,11 @@ extern const char bb_default_login_shell[];
1606 1606
1607/* We save ~500 bytes on isdigit alone. 1607/* We save ~500 bytes on isdigit alone.
1608 * BTW, x86 likes (unsigned char) cast more than (unsigned). */ 1608 * BTW, x86 likes (unsigned char) cast more than (unsigned). */
1609#define isdigit(a) ((unsigned char)((a) - '0') <= 9) 1609
1610/* These work the same for ASCII and Unicode,
1611 * assuming no one asks "is this a *Unicode* letter?" using isalpha(letter) */
1610#define isascii(a) ((unsigned char)(a) <= 0x7f) 1612#define isascii(a) ((unsigned char)(a) <= 0x7f)
1611#define isgraph(a) ((unsigned char)(a) > ' ') 1613#define isdigit(a) ((unsigned char)((a) - '0') <= 9)
1612#define isprint(a) ((unsigned char)(a) >= ' ')
1613#define isupper(a) ((unsigned char)((a) - 'A') <= ('Z' - 'A')) 1614#define isupper(a) ((unsigned char)((a) - 'A') <= ('Z' - 'A'))
1614#define islower(a) ((unsigned char)((a) - 'a') <= ('z' - 'a')) 1615#define islower(a) ((unsigned char)((a) - 'a') <= ('z' - 'a'))
1615#define isalpha(a) ((unsigned char)(((a)|0x20) - 'a') <= ('z' - 'a')) 1616#define isalpha(a) ((unsigned char)(((a)|0x20) - 'a') <= ('z' - 'a'))
@@ -1619,9 +1620,9 @@ extern const char bb_default_login_shell[];
1619 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13. 1620 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
1620 */ 1621 */
1621#define isspace(a) ({ unsigned char bb__isspace = (a) - 9; bb__isspace == (' ' - 9) || bb__isspace <= (13 - 9); }) 1622#define isspace(a) ({ unsigned char bb__isspace = (a) - 9; bb__isspace == (' ' - 9) || bb__isspace <= (13 - 9); })
1622 1623// Unsafe wrt NUL: #define ispunct(a) (strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", (a)) != NULL)
1623// Bigger code: 1624#define ispunct(a) (strchrnul("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", (a))[0])
1624//#define isalnum(a) ({ unsigned char bb__isalnum = (a) - '0'; bb__isalnum <= 9 || ((bb__isalnum - ('A' - '0')) & 0xdf) <= 25; }) 1625// Bigger code: #define isalnum(a) ({ unsigned char bb__isalnum = (a) - '0'; bb__isalnum <= 9 || ((bb__isalnum - ('A' - '0')) & 0xdf) <= 25; })
1625#define isalnum(a) bb_ascii_isalnum(a) 1626#define isalnum(a) bb_ascii_isalnum(a)
1626static ALWAYS_INLINE int bb_ascii_isalnum(unsigned char a) 1627static ALWAYS_INLINE int bb_ascii_isalnum(unsigned char a)
1627{ 1628{
@@ -1640,11 +1641,6 @@ static ALWAYS_INLINE int bb_ascii_isxdigit(unsigned char a)
1640 b = (a|0x20) - 'a'; 1641 b = (a|0x20) - 'a';
1641 return b <= 'f' - 'a'; 1642 return b <= 'f' - 'a';
1642} 1643}
1643
1644// Unsafe wrt NUL!
1645//#define ispunct(a) (strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", (a)) != NULL)
1646#define ispunct(a) (strchrnul("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", (a))[0])
1647
1648#define toupper(a) bb_ascii_toupper(a) 1644#define toupper(a) bb_ascii_toupper(a)
1649static ALWAYS_INLINE unsigned char bb_ascii_toupper(unsigned char a) 1645static ALWAYS_INLINE unsigned char bb_ascii_toupper(unsigned char a)
1650{ 1646{
@@ -1662,6 +1658,14 @@ static ALWAYS_INLINE unsigned char bb_ascii_tolower(unsigned char a)
1662 return a; 1658 return a;
1663} 1659}
1664 1660
1661/* In ASCII and Unicode, these are likely to be very different.
1662 * Let's prevent ambiguous usage from the start */
1663#define isgraph(a) isgraph_is_ambiguous_dont_use(a)
1664#define isprint(a) isprint_is_ambiguous_dont_use(a)
1665/* NB: must not treat EOF as isgraph or isprint */
1666#define isgraph_asciionly(a) ((unsigned)((a) - 0x21) <= 0x7e - 0x21)
1667#define isprint_asciionly(a) ((unsigned)((a) - 0x20) <= 0x7e - 0x20)
1668
1665 1669
1666POP_SAVED_FUNCTION_VISIBILITY 1670POP_SAVED_FUNCTION_VISIBILITY
1667 1671
diff --git a/libbb/dump.c b/libbb/dump.c
index bef485eff..49e5e26f8 100644
--- a/libbb/dump.c
+++ b/libbb/dump.c
@@ -492,13 +492,13 @@ static void conv_c(PR *pr, unsigned char *p)
492 str += 4; 492 str += 4;
493 } while (*str); 493 } while (*str);
494 494
495 if (isprint(*p)) { 495 if (isprint_asciionly(*p)) {
496 *pr->cchar = 'c'; 496 *pr->cchar = 'c';
497 printf(pr->fmt, *p); 497 printf(pr->fmt, *p);
498 } else { 498 } else {
499 sprintf(buf, "%03o", (int) *p); 499 sprintf(buf, "%03o", (int) *p);
500 str = buf; 500 str = buf;
501 strpr: 501 strpr:
502 *pr->cchar = 's'; 502 *pr->cchar = 's';
503 printf(pr->fmt, str); 503 printf(pr->fmt, str);
504 } 504 }
@@ -519,7 +519,7 @@ static void conv_u(PR *pr, unsigned char *p)
519 } else if (*p == 0x7f) { 519 } else if (*p == 0x7f) {
520 *pr->cchar = 's'; 520 *pr->cchar = 's';
521 printf(pr->fmt, "del"); 521 printf(pr->fmt, "del");
522 } else if (isprint(*p)) { 522 } else if (*p < 0x7f) { /* isprint() */
523 *pr->cchar = 'c'; 523 *pr->cchar = 'c';
524 printf(pr->fmt, *p); 524 printf(pr->fmt, *p);
525 } else { 525 } else {
@@ -609,7 +609,7 @@ static void display(priv_dumper_t* dumper)
609 break; 609 break;
610 } 610 }
611 case F_P: 611 case F_P:
612 printf(pr->fmt, isprint(*bp) ? *bp : '.'); 612 printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
613 break; 613 break;
614 case F_STR: 614 case F_STR:
615 printf(pr->fmt, (char *) bp); 615 printf(pr->fmt, (char *) bp);
diff --git a/loginutils/getty.c b/loginutils/getty.c
index f44d387b6..20411b04c 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -487,7 +487,7 @@ static char *get_logname(char *logname, unsigned size_logname,
487 case CTL('D'): 487 case CTL('D'):
488 exit(EXIT_SUCCESS); 488 exit(EXIT_SUCCESS);
489 default: 489 default:
490 if (!isprint(ascval)) { 490 if (ascval < ' ') {
491 /* ignore garbage characters */ 491 /* ignore garbage characters */
492 } else if ((int)(bp - logname) >= size_logname - 1) { 492 } else if ((int)(bp - logname) >= size_logname - 1) {
493 bb_error_msg_and_die("%s: input overrun", op->tty); 493 bb_error_msg_and_die("%s: input overrun", op->tty);
diff --git a/miscutils/strings.c b/miscutils/strings.c
index fea9edbed..b4c5854cf 100644
--- a/miscutils/strings.c
+++ b/miscutils/strings.c
@@ -49,7 +49,7 @@ int strings_main(int argc UNUSED_PARAM, char **argv)
49 count = 0; 49 count = 0;
50 do { 50 do {
51 c = fgetc(file); 51 c = fgetc(file);
52 if (isprint(c) || c == '\t') { 52 if (isprint_asciionly(c) || c == '\t') {
53 if (count > n) { 53 if (count > n) {
54 bb_putchar(c); 54 bb_putchar(c);
55 } else { 55 } else {
diff --git a/networking/inetd.c b/networking/inetd.c
index 391bb9ba6..a45573396 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -1475,9 +1475,8 @@ static void init_ring(void)
1475 int i; 1475 int i;
1476 1476
1477 end_ring = ring; 1477 end_ring = ring;
1478 for (i = 0; i <= 128; ++i) 1478 for (i = ' '; i < 127; i++)
1479 if (isprint(i)) 1479 *end_ring++ = i;
1480 *end_ring++ = i;
1481} 1480}
1482/* Character generator. MMU arches only. */ 1481/* Character generator. MMU arches only. */
1483/* ARGSUSED */ 1482/* ARGSUSED */