Assert that unicode are limited to 0-10FFFF.

This commit is contained in:
Renaud 2015-02-08 14:26:07 +08:00
parent 2cef071492
commit bdbd2d5437
3 changed files with 22 additions and 11 deletions

17
eval.c
View File

@ -523,10 +523,19 @@ static char *gtfun( char *fname) {
}
break ;
case UFCHR:
sz = unicode_to_utf8( atoi( argx), result) ;
result[ sz] = 0 ;
retstr = result ;
case UFCHR: {
unicode_t c ;
c = atoi( argx) ;
if( c > 0x10FFFF)
retstr = errorm ;
else {
sz = unicode_to_utf8( c, result) ;
result[ sz] = 0 ;
retstr = result ;
}
}
break ;
case UFGTKEY:
result[0] = tgetc();

10
utf8.c
View File

@ -1,5 +1,7 @@
#include "utf8.h"
#include <assert.h>
/*
* utf8_to_unicode()
*
@ -84,11 +86,11 @@ static void reverse_string(char *begin, char *end)
* possible sequence, while utf8_to_unicode() accepts both Latin1 and
* overlong utf-8 sequences.
*/
unsigned unicode_to_utf8(unsigned int c, char *utf8)
{
int bytes = 1;
unsigned unicode_to_utf8( unicode_t c, char *utf8) {
int bytes = 1 ;
*utf8 = c;
assert( c <= 0x10FFFF) ;
*utf8 = c ;
if (c > 0x7f) {
int prefix = 0x40;
char *p = utf8;

6
utf8.h
View File

@ -1,10 +1,10 @@
#ifndef UTF8_H
#define UTF8_H
typedef unsigned int unicode_t;
typedef unsigned int unicode_t ;
unsigned utf8_to_unicode( char *line, unsigned index, unsigned len,
unicode_t *res) ;
unsigned unicode_to_utf8( unsigned int c, char *utf8) ;
unicode_t *res) ;
unsigned unicode_to_utf8( unicode_t c, char *utf8) ;
#endif