From 781f0a39aaa38f7f8191822bdc29fb181d1f0fe7 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sun, 7 Jun 2020 22:51:45 +0200 Subject: [PATCH] Fix parsing of multibyte characters According to Red Hat, this fixes "a bug on input buffer boundary and/or temporary composing buffer of multibyte characters". The patch was credited to Paulo Andrade . To be honest, I don't know how to trigger this bug or what the code removed by this fix really does, but this patch is in production use at Red Hat, removes some smelly stuff, and is not triggering any regression test failures, so I'll just take this one on faith. https://bugzilla.redhat.com/show_bug.cgi?id=1417886 https://github.com/att/ast/commit/4fa2020b src/cmd/ksh93/sh/fcin.c: - _fcmbget(): Remove some dodgy-looking buffer-fiddling code that is marked as "for testing purposes with small buffers". (cherry picked from commit 407760fdbddcb7f8ac92b5d1da29d3e09dac0369) --- src/cmd/ksh93/sh/fcin.c | 46 +---------------------------------------- 1 file changed, 1 insertion(+), 45 deletions(-) diff --git a/src/cmd/ksh93/sh/fcin.c b/src/cmd/ksh93/sh/fcin.c index ea6ea6be6..e2566bc0e 100644 --- a/src/cmd/ksh93/sh/fcin.c +++ b/src/cmd/ksh93/sh/fcin.c @@ -150,56 +150,12 @@ extern void fcrestore(Fcin_t *fp) _Fcin = *fp; } -/* for testing purposes with small buffers */ -#if defined(IOBSIZE) && (IOBSIZE < 2*MB_LEN_MAX) -# undef MB_LEN_MAX -# define MB_LEN_MAX (IOBSIZE/2) -#endif - -struct Extra -{ - unsigned char buff[2*MB_LEN_MAX]; - unsigned char *next; -}; - int _fcmbget(short *len) { - static struct Extra extra; - register int i, c, n; - if(_Fcin.fcleft) - { - if((c = mbsize(extra.next)) < 0) - c = 1; - if((_Fcin.fcleft -= c) <=0) - { - _Fcin.fcptr = (unsigned char*)fcfirst() - _Fcin.fcleft; - _Fcin.fcleft = 0; - } - *len = c; - if(c==1) - c = *extra.next++; - else if(c==0) - _Fcin.fcleft = 0; - else - c = mbchar(extra.next); - return(c); - } + register int c; switch(*len = mbsize(_Fcin.fcptr)) { case -1: - if(_Fcin._fcfile && (n=(_Fcin.fclast-_Fcin.fcptr)) < MB_LEN_MAX) - { - memcpy(extra.buff, _Fcin.fcptr, n); - _Fcin.fcptr = _Fcin.fclast; - for(i=n; i < MB_LEN_MAX+n; i++) - { - if((extra.buff[i] = fcgetc(c))==0) - break; - } - _Fcin.fcleft = n; - extra.next = extra.buff; - return(fcmbget(len)); - } *len = 1; /* fall through */ case 0: