Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit93e8ff8

Browse files
committed
Refactor logic to check for ASCII-only characters in string
The same logic was present for collation commands, SASLprep andpgcrypto, so this removes some code.Author: Michael PaquierReviewed-by: Stephen Frost, Heikki LinnakangasDiscussion:https://postgr.es/m/X9womIn6rne6Gud2@paquier.xyz
1 parent4e1ee79 commit93e8ff8

File tree

5 files changed

+26
-52
lines changed

5 files changed

+26
-52
lines changed

‎contrib/pgcrypto/pgp-pgsql.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include"postgres.h"
3333

3434
#include"catalog/pg_type.h"
35+
#include"common/string.h"
3536
#include"funcapi.h"
3637
#include"lib/stringinfo.h"
3738
#include"mb/pg_wchar.h"
@@ -92,19 +93,6 @@ convert_to_utf8(text *src)
9293
returnconvert_charset(src,GetDatabaseEncoding(),PG_UTF8);
9394
}
9495

95-
staticbool
96-
string_is_ascii(constchar*str)
97-
{
98-
constchar*p;
99-
100-
for (p=str;*p;p++)
101-
{
102-
if (IS_HIGHBIT_SET(*p))
103-
return false;
104-
}
105-
return true;
106-
}
107-
10896
staticvoid
10997
clear_and_pfree(text*p)
11098
{
@@ -814,7 +802,7 @@ parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array,
814802

815803
v=TextDatumGetCString(key_datums[i]);
816804

817-
if (!string_is_ascii(v))
805+
if (!pg_is_ascii(v))
818806
ereport(ERROR,
819807
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
820808
errmsg("header key must not contain non-ASCII characters")));
@@ -836,7 +824,7 @@ parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array,
836824

837825
v=TextDatumGetCString(val_datums[i]);
838826

839-
if (!string_is_ascii(v))
827+
if (!pg_is_ascii(v))
840828
ereport(ERROR,
841829
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
842830
errmsg("header value must not contain non-ASCII characters")));

‎src/backend/commands/collationcmds.c

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include"commands/comment.h"
2828
#include"commands/dbcommands.h"
2929
#include"commands/defrem.h"
30+
#include"common/string.h"
3031
#include"mb/pg_wchar.h"
3132
#include"miscadmin.h"
3233
#include"utils/acl.h"
@@ -286,23 +287,6 @@ pg_collation_actual_version(PG_FUNCTION_ARGS)
286287
#defineREAD_LOCALE_A_OUTPUT
287288
#endif
288289

289-
#if defined(READ_LOCALE_A_OUTPUT)|| defined(USE_ICU)
290-
/*
291-
* Check a string to see if it is pure ASCII
292-
*/
293-
staticbool
294-
is_all_ascii(constchar*str)
295-
{
296-
while (*str)
297-
{
298-
if (IS_HIGHBIT_SET(*str))
299-
return false;
300-
str++;
301-
}
302-
return true;
303-
}
304-
#endif/* READ_LOCALE_A_OUTPUT || USE_ICU */
305-
306290
#ifdefREAD_LOCALE_A_OUTPUT
307291
/*
308292
* "Normalize" a libc locale name, stripping off encoding tags such as
@@ -396,7 +380,7 @@ get_icu_locale_comment(const char *localename)
396380
if (U_FAILURE(status))
397381
returnNULL;/* no good reason to raise an error */
398382

399-
/* Check for non-ASCII comment (can't useis_all_ascii for this) */
383+
/* Check for non-ASCII comment (can't usepg_is_ascii for this) */
400384
for (i=0;i<len_uchar;i++)
401385
{
402386
if (displayname[i]>127)
@@ -477,7 +461,7 @@ pg_import_system_collations(PG_FUNCTION_ARGS)
477461
* interpret the non-ASCII characters. We can't do much with
478462
* those, so we filter them out.
479463
*/
480-
if (!is_all_ascii(localebuf))
464+
if (!pg_is_ascii(localebuf))
481465
{
482466
elog(DEBUG1,"locale name has non-ASCII characters, skipped: \"%s\"",localebuf);
483467
continue;
@@ -623,7 +607,7 @@ pg_import_system_collations(PG_FUNCTION_ARGS)
623607
* Be paranoid about not allowing any non-ASCII strings into
624608
* pg_collation
625609
*/
626-
if (!is_all_ascii(langtag)|| !is_all_ascii(collcollate))
610+
if (!pg_is_ascii(langtag)|| !pg_is_ascii(collcollate))
627611
continue;
628612

629613
collid=CollationCreate(psprintf("%s-x-icu",langtag),

‎src/common/saslprep.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#endif
2727

2828
#include"common/saslprep.h"
29+
#include"common/string.h"
2930
#include"common/unicode_norm.h"
3031
#include"mb/pg_wchar.h"
3132

@@ -47,7 +48,6 @@
4748
staticintcodepoint_range_cmp(constvoid*a,constvoid*b);
4849
staticboolis_code_in_table(pg_wcharcode,constpg_wchar*map,intmapsize);
4950
staticintpg_utf8_string_len(constchar*source);
50-
staticboolpg_is_ascii_string(constchar*p);
5151

5252
/*
5353
* Stringprep Mapping Tables.
@@ -1019,21 +1019,6 @@ pg_utf8_string_len(const char *source)
10191019
returnnum_chars;
10201020
}
10211021

1022-
/*
1023-
* Returns true if the input string is pure ASCII.
1024-
*/
1025-
staticbool
1026-
pg_is_ascii_string(constchar*p)
1027-
{
1028-
while (*p)
1029-
{
1030-
if (IS_HIGHBIT_SET(*p))
1031-
return false;
1032-
p++;
1033-
}
1034-
return true;
1035-
}
1036-
10371022

10381023
/*
10391024
* pg_saslprep - Normalize a password with SASLprep.
@@ -1076,7 +1061,7 @@ pg_saslprep(const char *input, char **output)
10761061
* Quick check if the input is pure ASCII. An ASCII string requires no
10771062
* further processing.
10781063
*/
1079-
if (pg_is_ascii_string(input))
1064+
if (pg_is_ascii(input))
10801065
{
10811066
*output=STRDUP(input);
10821067
if (!(*output))

‎src/common/string.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ pg_clean_ascii(char *str)
9292
}
9393

9494

95+
/*
96+
* pg_is_ascii -- Check if string is made only of ASCII characters
97+
*/
98+
bool
99+
pg_is_ascii(constchar*str)
100+
{
101+
while (*str)
102+
{
103+
if (IS_HIGHBIT_SET(*str))
104+
return false;
105+
str++;
106+
}
107+
return true;
108+
}
109+
110+
95111
/*
96112
* pg_strip_crlf -- Remove any trailing newline and carriage return
97113
*

‎src/include/common/string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern intstrtoint(const char *pg_restrict str, char **pg_restrict endptr,
1818
intbase);
1919
externvoidpg_clean_ascii(char*str);
2020
externintpg_strip_crlf(char*str);
21+
externboolpg_is_ascii(constchar*str);
2122

2223
/* functions in src/common/pg_get_line.c */
2324
externchar*pg_get_line(FILE*stream);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp