[hackers] [libutf] reject bad runes in runelen, runetochar || Connor Lane Smith
changeset: 15:bcf6cc6b9f2d
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Mon May 21 17:57:46 2012 +0100
files: rune.c utf.h
description:
reject bad runes in runelen, runetochar
diff -r 1d508c1d1549 -r bcf6cc6b9f2d rune.c
--- a/rune.c Mon May 21 15:29:42 2012 +0100
+++ b/rune.c Mon May 21 17:57:46 2012 +0100
_AT_@ -4,14 +4,19 @@
#define MIN(x,y) ((x) < (y) ? (x) : (y))
-#define UTFSEQ(x) ((((x) & 0x80) == 0x00) ? 1 /* 0xxxxxxx */ \
- : (((x) & 0xC0) == 0x80) ? 0 /* 10xxxxxx */ \
- : (((x) & 0xE0) == 0xC0) ? 2 /* 110xxxxx */ \
- : (((x) & 0xF0) == 0xE0) ? 3 /* 1110xxxx */ \
- : (((x) & 0xF8) == 0xF0) ? 4 /* 11110xxx */ \
- : (((x) & 0xFC) == 0xF8) ? 5 /* 111110xx */ \
- : (((x) & 0xFE) == 0xFC) ? 6 /* 1111110x */ \
- : 0 )
+#define UTFSEQ(x) ((((x) & 0x80) == 0x00) ? 1 /* 0xxxxxxx */ \
+ : (((x) & 0xC0) == 0x80) ? 0 /* 10xxxxxx */ \
+ : (((x) & 0xE0) == 0xC0) ? 2 /* 110xxxxx */ \
+ : (((x) & 0xF0) == 0xE0) ? 3 /* 1110xxxx */ \
+ : (((x) & 0xF8) == 0xF0) ? 4 /* 11110xxx */ \
+ : (((x) & 0xFC) == 0xF8) ? 5 /* 111110xx */ \
+ : (((x) & 0xFE) == 0xFC) ? 6 /* 1111110x */ \
+ : 0 )
+
+#define BADRUNE(x) ((x) > Runemax \
+ || ((x) & 0xFFFE) == 0xFFFE \
+ || ((x) >= 0xD800 && (x) <= 0xDFFF) \
+ || ((x) >= 0xFDD0 && (x) <= 0xFDEF))
/*
* runetochar copies one rune at p to at most UTFmax bytes starting at s and
_AT_@ -21,7 +26,7 @@
* If the rune is illegal, runetochar will return 0.
*/
int
-runetochar(char *s, Rune *p)
+runetochar(char *s, const Rune *p)
{
Rune r = *p;
_AT_@ -104,8 +109,7 @@
return 0;
/* reject invalid runes and overlong sequences */
- if(n > UTFmax || r > 0x10FFFF || runelen(r) < (int)n || (r & 0xFFFE) == 0xFFFE
- || (r >= 0xD800 && r <= 0xDFFF) || (r >= 0xFDD0 && r <= 0xFDEF))
+ if(n > UTFmax || runelen(r) < (int)n || BADRUNE(r))
r = Runeerror;
*p = r;
_AT_@ -119,16 +123,16 @@
int
runelen(Rune r)
{
- if(r <= 0x7F)
+ if(BADRUNE(r))
+ return 0; /* error */
+ else if(r <= 0x7F)
return 1;
else if(r <= 0x07FF)
return 2;
else if(r <= 0xFFFF)
return 3;
- else if(r <= Runemax)
+ else
return 4;
- else
- return 0; /* error */
}
/*
_AT_@ -136,7 +140,7 @@
* length len pointed to by p into UTF-8.
*/
size_t
-runenlen(Rune *p, size_t len)
+runenlen(const Rune *p, size_t len)
{
size_t i, n = 0;
diff -r 1d508c1d1549 -r bcf6cc6b9f2d utf.h
--- a/utf.h Mon May 21 15:29:42 2012 +0100
+++ b/utf.h Mon May 21 17:57:46 2012 +0100
_AT_@ -13,10 +13,11 @@
Runemax = 0x10FFFF /* maximum rune value */
};
-int runetochar(char *, Rune *);
+int runetochar(char *, const Rune *);
int chartorune(Rune *, const char *);
int charntorune(Rune *, const char *, size_t);
-int runelen(Rune);
+int runelen(const Rune);
+size_t runenlen(const Rune *, size_t);
int fullrune(const char *, size_t);
char *utfecpy(char *, char *, const char *);
size_t utflen(const char *);
Received on Mon May 21 2012 - 20:02:21 CEST
This archive was generated by hypermail 2.3.0
: Mon May 21 2012 - 20:12:07 CEST