1 /*-------------------------------------------------------------------------
4 * string handling helpers
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
14 *-------------------------------------------------------------------------
21 #include "postgres_fe.h"
24 #include "common/string.h"
28 * Returns whether the string `str' has the postfix `end'.
31 pg_str_endswith(const char *str
, const char *end
)
33 size_t slen
= strlen(str
);
34 size_t elen
= strlen(end
);
36 /* can't be a postfix if longer */
40 /* compare the end of the strings */
42 return strcmp(str
, end
) == 0;
47 * strtoint --- just like strtol, but returns int not long
50 strtoint(const char *pg_restrict str
, char **pg_restrict endptr
, int base
)
54 val
= strtol(str
, endptr
, base
);
62 * pg_clean_ascii -- Replace any non-ASCII chars with a "\xXX" string
64 * Makes a newly allocated copy of the string passed in, which must be
65 * '\0'-terminated. In the backend, additional alloc_flags may be provided and
66 * will be passed as-is to palloc_extended(); in the frontend, alloc_flags is
67 * ignored and the copy is malloc'd.
69 * This function exists specifically to deal with filtering out
70 * non-ASCII characters in a few places where the client can provide an almost
71 * arbitrary string (and it isn't checked to ensure it's a valid username or
72 * database name or similar) and we don't want to have control characters or other
73 * things ending up in the log file where server admins might end up with a
74 * messed up terminal when looking at them.
76 * In general, this function should NOT be used- instead, consider how to handle
77 * the string without needing to filter out the non-ASCII characters.
79 * Ultimately, we'd like to improve the situation to not require replacing all
80 * non-ASCII but perform more intelligent filtering which would allow UTF or
81 * similar, but it's unclear exactly what we should allow, so stick to ASCII only
85 pg_clean_ascii(const char *str
, int alloc_flags
)
92 /* Worst case, each byte can become four bytes, plus a null terminator. */
93 dstlen
= strlen(str
) * 4 + 1;
98 dst
= palloc_extended(dstlen
, alloc_flags
);
104 for (p
= str
; *p
!= '\0'; p
++)
107 /* Only allow clean ASCII chars in the string */
108 if (*p
< 32 || *p
> 126)
110 Assert(i
< (dstlen
- 3));
111 snprintf(&dst
[i
], dstlen
- i
, "\\x%02x", (unsigned char) *p
);
129 * pg_is_ascii -- Check if string is made only of ASCII characters
132 pg_is_ascii(const char *str
)
136 if (IS_HIGHBIT_SET(*str
))
145 * pg_strip_crlf -- Remove any trailing newline and carriage return
147 * Removes any trailing newline and carriage return characters (\r on
148 * Windows) in the input string, zero-terminating it.
150 * The passed in string must be zero-terminated. This function returns
151 * the new length of the string.
154 pg_strip_crlf(char *str
)
156 int len
= strlen(str
);
158 while (len
> 0 && (str
[len
- 1] == '\n' ||
159 str
[len
- 1] == '\r'))