Skip to content

Commit 93e8ff8

Browse files
committed
Refactor logic to check for ASCII-only characters in string
The same logic was present for collation commands, SASLprep and pgcrypto, so this removes some code. Author: Michael Paquier Reviewed-by: Stephen Frost, Heikki Linnakangas Discussion: https://postgr.es/m/X9womIn6rne6Gud2@paquier.xyz
1 parent 4e1ee79 commit 93e8ff8

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
return convert_charset(src, GetDatabaseEncoding(), PG_UTF8);
9394
}
9495

95-
static bool
96-
string_is_ascii(const char *str)
97-
{
98-
const char *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
static void
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
#define READ_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-
static bool
294-
is_all_ascii(const char *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
#ifdef READ_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
return NULL; /* no good reason to raise an error */
398382

399-
/* Check for non-ASCII comment (can't use is_all_ascii for this) */
383+
/* Check for non-ASCII comment (can't use pg_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
static int codepoint_range_cmp(const void *a, const void *b);
4849
static bool is_code_in_table(pg_wchar code, const pg_wchar *map, int mapsize);
4950
static int pg_utf8_string_len(const char *source);
50-
static bool pg_is_ascii_string(const char *p);
5151

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

1022-
/*
1023-
* Returns true if the input string is pure ASCII.
1024-
*/
1025-
static bool
1026-
pg_is_ascii_string(const char *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(const char *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 int strtoint(const char *pg_restrict str, char **pg_restrict endptr,
1818
int base);
1919
extern void pg_clean_ascii(char *str);
2020
extern int pg_strip_crlf(char *str);
21+
extern bool pg_is_ascii(const char *str);
2122

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

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