Skip to content

Commit 56f5d34

Browse files
committed
Fix some wide-character bugs in the text-search parser.
In p_isdigit and other character class test functions generated by the p_iswhat macro, the code path for non-C locales with multibyte encodings contained a bogus pointer cast that would accidentally fail to malfunction if types wchar_t and wint_t have the same width. Apparently that is true on most platforms, but not on recent Cygwin releases. Remove the cast, as it seems completely unnecessary (I think it arose from a false analogy to the need to cast to unsigned char when dealing with the <ctype.h> functions). Per bug #8970 from Marco Atzeri. In the same functions, the code path for C locale with a multibyte encoding simply ANDed each wide character with 0xFF before passing it to the corresponding <ctype.h> function. This could result in false positive answers for some non-ASCII characters, so use a range test instead. Noted by me while investigating Marco's complaint. Also, remove some useless though not actually buggy maskings and casts in the hand-coded p_isalnum and p_isalpha functions, which evidently got tested a bit more carefully than the macro-generated functions.
1 parent ae3c98b commit 56f5d34

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/backend/tsearch/wparser_def.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ TParserClose(TParser *prs)
356356
* or give wrong result.
357357
* - multibyte encoding and C-locale often are used for
358358
* Asian languages.
359-
* - if locale is C the we use pgwstr instead of wstr
359+
* - if locale is C then we use pgwstr instead of wstr.
360360
*/
361361

362362
#ifdef USE_WIDE_UPPER_LOWER
@@ -368,9 +368,13 @@ p_is##type(TParser *prs) { \
368368
if ( prs->usewide ) \
369369
{ \
370370
if ( prs->pgwstr ) \
371-
return is##type( 0xff & *( prs->pgwstr + prs->state->poschar) );\
372-
\
373-
return isw##type( *(wint_t*)( prs->wstr + prs->state->poschar ) ); \
371+
{ \
372+
unsigned int c = *(prs->pgwstr + prs->state->poschar); \
373+
if ( c > 0x7f ) \
374+
return 0; \
375+
return is##type( c ); \
376+
} \
377+
return isw##type( *( prs->wstr + prs->state->poschar ) ); \
374378
} \
375379
\
376380
return is##type( *(unsigned char*)( prs->str + prs->state->posbyte ) ); \
@@ -399,10 +403,10 @@ p_isalnum(TParser *prs)
399403
if (c > 0x7f)
400404
return 1;
401405

402-
return isalnum(0xff & c);
406+
return isalnum(c);
403407
}
404408

405-
return iswalnum((wint_t) *(prs->wstr + prs->state->poschar));
409+
return iswalnum(*(prs->wstr + prs->state->poschar));
406410
}
407411

408412
return isalnum(*(unsigned char *) (prs->str + prs->state->posbyte));
@@ -431,10 +435,10 @@ p_isalpha(TParser *prs)
431435
if (c > 0x7f)
432436
return 1;
433437

434-
return isalpha(0xff & c);
438+
return isalpha(c);
435439
}
436440

437-
return iswalpha((wint_t) *(prs->wstr + prs->state->poschar));
441+
return iswalpha(*(prs->wstr + prs->state->poschar));
438442
}
439443

440444
return isalpha(*(unsigned char *) (prs->str + prs->state->posbyte));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy