From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A55933858C2D; Wed, 16 Feb 2022 06:41:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A55933858C2D From: "mbuilov at gmail dot com" To: glibc-bugs@sourceware.org Subject: [Bug string/28898] New: mbrtoc16 segfaults when counting wide characters Date: Wed, 16 Feb 2022 06:41:00 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: glibc X-Bugzilla-Component: string X-Bugzilla-Version: unspecified X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: mbuilov at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: glibc-bugs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Glibc-bugs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Feb 2022 06:41:00 -0000 https://sourceware.org/bugzilla/show_bug.cgi?id=3D28898 Bug ID: 28898 Summary: mbrtoc16 segfaults when counting wide characters Product: glibc Version: unspecified Status: UNCONFIRMED Severity: normal Priority: P2 Component: string Assignee: unassigned at sourceware dot org Reporter: mbuilov at gmail dot com Target Milestone: --- The following test case demonstrates Segmentation fault. The program works correctly if the first argument to mbrtoc16() is not NULL, otherwise it crashes. $ cat bug.c #include #include #include #include static const unsigned char utf8[] =3D { 0xf0, 0x9d, 0x94, 0xa0, 0xd5, 0xae }; int main(int argc, char *argv[]) { mbstate_t state; unsigned i =3D 0, n =3D 0; const char *const locale =3D setlocale(LC_ALL, "C.UTF-8"); if (!locale) { fprintf(stderr, "failed to set locale\n"); return -2; } memset(&state, 0, sizeof(state)); while (i < sizeof(utf8)) { const size_t sz =3D mbrtoc16(NULL, (const char*)&utf8[i], 1, &state); if (sz =3D=3D (size_t)-2) { i +=3D 1; continue; } if (sz !=3D (size_t)-3) i +=3D 1; n++; } fprintf(stdout, "number of utf16 characters: %u\n", n); (void)argc, (void)argv; return 0; } ------------- The problem is in the following peace of code of mbrtoc16(), in ./wcsmbs/mbrtoc16.c: /* The standard text does not say that S being NULL means the state is reset even if the second half of a surrogate still have to be returned. In fact, the error code description indicates otherwise. Therefore always first try to return a second half. */ if (ps->__count & 0x80000000) { /* We have to return the second word for a surrogate. */ ps->__count &=3D 0x7fffffff; *pc16 =3D ps->__value.__wch; ps->__value.__wch =3D L'\0'; return (size_t) -3; } - the pc16 pointer is not checked for NULL before being dereferenced. --=20 You are receiving this mail because: You are on the CC list for the bug.=