Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3a671e1

Browse files
committed
Fix global ICU collations for ICU < 54
createdb() didn't check for collation attributes validity, which has to be done explicitly on ICU < 54. It also forgot to close the ICU collator opened during the check which leaks some memory. To fix both, add a new check_icu_locale() that does all the appropriate verification and close the ICU collator. initdb also had some partial check for ICU < 54. To have consistent error reporting across major ICU versions, and get rid of the need to include ucol.h, remove the partial check there. The backend will report an error if needed during the post-boostrap iniitialization phase. Author: Julien Rouhaud <julien.rouhaud@free.fr> Discussion: https://www.postgresql.org/message-id/20220319041459.qqqiqh335sga5ezj@jrouhaud
1 parent 3c0c5cc commit 3a671e1

File tree

7 files changed

+37
-40
lines changed

7 files changed

+37
-40
lines changed

src/backend/commands/dbcommands.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -458,23 +458,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
458458
}
459459

460460
if (dblocprovider == COLLPROVIDER_ICU)
461-
{
462-
#ifdef USE_ICU
463-
UErrorCode status;
464-
465-
status = U_ZERO_ERROR;
466-
ucol_open(dbiculocale, &status);
467-
if (U_FAILURE(status))
468-
ereport(ERROR,
469-
(errmsg("could not open collator for locale \"%s\": %s",
470-
dbiculocale, u_errorName(status))));
471-
#else
472-
ereport(ERROR,
473-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
474-
errmsg("ICU is not supported in this build"), \
475-
errhint("You need to rebuild PostgreSQL using %s.", "--with-icu")));
476-
#endif
477-
}
461+
check_icu_locale(dbiculocale);
478462

479463
/*
480464
* Check that the new encoding and locale settings match the source

src/backend/utils/adt/pg_locale.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,34 @@ icu_set_collation_attributes(UCollator *collator, const char *loc)
19851985

19861986
#endif /* USE_ICU */
19871987

1988+
/*
1989+
* Check if the given locale ID is valid, and ereport(ERROR) if it isn't.
1990+
*/
1991+
void
1992+
check_icu_locale(const char *icu_locale)
1993+
{
1994+
#ifdef USE_ICU
1995+
UCollator *collator;
1996+
UErrorCode status;
1997+
1998+
status = U_ZERO_ERROR;
1999+
collator = ucol_open(icu_locale, &status);
2000+
if (U_FAILURE(status))
2001+
ereport(ERROR,
2002+
(errmsg("could not open collator for locale \"%s\": %s",
2003+
icu_locale, u_errorName(status))));
2004+
2005+
if (U_ICU_VERSION_MAJOR_NUM < 54)
2006+
icu_set_collation_attributes(collator, icu_locale);
2007+
ucol_close(collator);
2008+
#else
2009+
ereport(ERROR,
2010+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2011+
errmsg("ICU is not supported in this build"), \
2012+
errhint("You need to rebuild PostgreSQL using %s.", "--with-icu")));
2013+
#endif
2014+
}
2015+
19882016
/*
19892017
* These functions convert from/to libc's wchar_t, *not* pg_wchar_t.
19902018
* Therefore we keep them here rather than with the mbutils code.

src/bin/initdb/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ OBJS = \
4040
all: initdb
4141

4242
initdb: $(OBJS) | submake-libpq submake-libpgport submake-libpgfeutils
43-
$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) $(ICU_LIBS) -o $@$(X)
43+
$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
4444

4545
# We must pull in localtime.c from src/timezones
4646
localtime.c: % : $(top_srcdir)/src/timezone/%

src/bin/initdb/initdb.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@
5555
#include <signal.h>
5656
#include <time.h>
5757

58-
#ifdef USE_ICU
59-
#include <unicode/ucol.h>
60-
#endif
61-
6258
#ifdef HAVE_SHM_OPEN
6359
#include "sys/mman.h"
6460
#endif
@@ -2205,22 +2201,10 @@ setlocales(void)
22052201
}
22062202

22072203
/*
2208-
* Check ICU locale ID
2204+
* In supported builds, the ICU locale ID will be checked by the
2205+
* backend when performing the post-boostrap initialization.
22092206
*/
2210-
#ifdef USE_ICU
2211-
{
2212-
UErrorCode status;
2213-
2214-
status = U_ZERO_ERROR;
2215-
ucol_open(icu_locale, &status);
2216-
if (U_FAILURE(status))
2217-
{
2218-
pg_log_error("could not open collator for locale \"%s\": %s",
2219-
icu_locale, u_errorName(status));
2220-
exit(1);
2221-
}
2222-
}
2223-
#else
2207+
#ifndef USE_ICU
22242208
pg_log_error("ICU is not supported in this build");
22252209
fprintf(stderr, _("You need to rebuild PostgreSQL using %s.\n"), "--with-icu");
22262210
exit(1);

src/bin/initdb/t/001_initdb.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
'option --icu-locale');
106106

107107
command_fails_like(['initdb', '--no-sync', '--locale-provider=icu', '--icu-locale=@colNumeric=lower', "$tempdir/dataX"],
108-
qr/initdb: error: could not open collator for locale/,
108+
qr/FATAL: could not open collator for locale/,
109109
'fails for invalid ICU locale');
110110
}
111111
else

src/include/utils/pg_locale.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ extern char *get_collation_actual_version(char collprovider, const char *collcol
116116
extern int32_t icu_to_uchar(UChar **buff_uchar, const char *buff, size_t nbytes);
117117
extern int32_t icu_from_uchar(char **result, const UChar *buff_uchar, int32_t len_uchar);
118118
#endif
119+
extern void check_icu_locale(const char *icu_locale);
119120

120121
/* These functions convert from/to libc's wchar_t, *not* pg_wchar_t */
121122
extern size_t wchar2char(char *to, const wchar_t *from, size_t tolen,

src/test/icu/t/010_database.pl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
$node1->start;
1717

1818
$node1->safe_psql('postgres',
19-
q{CREATE DATABASE dbicu LOCALE_PROVIDER icu LOCALE 'C' ICU_LOCALE 'en-u-kf-upper' ENCODING 'UTF8' TEMPLATE template0});
19+
q{CREATE DATABASE dbicu LOCALE_PROVIDER icu LOCALE 'C' ICU_LOCALE 'en@colCaseFirst=upper' ENCODING 'UTF8' TEMPLATE template0});
2020

2121
$node1->safe_psql('dbicu',
2222
q{
23-
CREATE COLLATION upperfirst (provider = icu, locale = 'en-u-kf-upper');
23+
CREATE COLLATION upperfirst (provider = icu, locale = 'en@colCaseFirst=upper');
2424
CREATE TABLE icu (def text, en text COLLATE "en-x-icu", upfirst text COLLATE upperfirst);
2525
INSERT INTO icu VALUES ('a', 'a', 'a'), ('b', 'b', 'b'), ('A', 'A', 'A'), ('B', 'B', 'B');
2626
});

0 commit comments

Comments
 (0)