Skip to content

Commit 98df8ba

Browse files
committed
Avoid breaking SJIS encoding while de-backslashing Windows paths.
When running on Windows, canonicalize_path() converts '\' to '/' to prevent confusing the Windows command processor. It was doing that in a non-encoding-aware fashion; but in SJIS there are valid two-byte characters whose second byte matches '\'. So encoding corruption ensues if such a character is used in the path. We can fairly easily fix this if we know which encoding is in use, but a lot of our utilities don't have much of a clue about that. After some discussion we decided we'd settle for fixing this only in psql, and assuming that its value of client_encoding matches what the user is typing. It seems hopeless to get the server to deal with the problematic characters in database path names, so we'll just declare that case to be unsupported. That means nothing need be done in the server, nor in utility programs whose only contact with file path names is for database paths. But psql frequently deals with client-side file paths, so it'd be good if it didn't mess those up. Bug: #18735 Reported-by: Koichi Suzuki <koichi.suzuki@enterprisedb.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Koichi Suzuki <koichi.suzuki@enterprisedb.com> Discussion: https://postgr.es/m/18735-4acdb3998bb9f2b1@postgresql.org Backpatch-through: 13
1 parent 8bfd2ce commit 98df8ba

File tree

4 files changed

+97
-19
lines changed

4 files changed

+97
-19
lines changed

src/bin/psql/command.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch,
10041004
{
10051005
expand_tilde(&fname);
10061006
if (fname)
1007-
canonicalize_path(fname);
1007+
canonicalize_path_enc(fname, pset.encoding);
10081008

10091009
/* If query_buf is empty, recall previous query for editing */
10101010
copy_previous_query(query_buf, previous_buf);
@@ -2577,7 +2577,7 @@ exec_command_write(PsqlScanState scan_state, bool active_branch,
25772577
}
25782578
else
25792579
{
2580-
canonicalize_path(fname);
2580+
canonicalize_path_enc(fname, pset.encoding);
25812581
fd = fopen(fname, "w");
25822582
}
25832583
if (!fd)
@@ -3923,7 +3923,7 @@ process_file(char *filename, bool use_relative_path)
39233923
}
39243924
else if (strcmp(filename, "-") != 0)
39253925
{
3926-
canonicalize_path(filename);
3926+
canonicalize_path_enc(filename, pset.encoding);
39273927

39283928
/*
39293929
* If we were asked to resolve the pathname relative to the location
@@ -3937,7 +3937,7 @@ process_file(char *filename, bool use_relative_path)
39373937
strlcpy(relpath, pset.inputfile, sizeof(relpath));
39383938
get_parent_directory(relpath);
39393939
join_path_components(relpath, relpath, filename);
3940-
canonicalize_path(relpath);
3940+
canonicalize_path_enc(relpath, pset.encoding);
39413941

39423942
filename = relpath;
39433943
}

src/bin/psql/copy.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ do_copy(const char *args)
280280

281281
/* prepare to read or write the target file */
282282
if (options->file && !options->program)
283-
canonicalize_path(options->file);
283+
canonicalize_path_enc(options->file, pset.encoding);
284284

285285
if (options->from)
286286
{

src/include/port.h

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern char *first_path_var_separator(const char *pathlist);
5050
extern void join_path_components(char *ret_path,
5151
const char *head, const char *tail);
5252
extern void canonicalize_path(char *path);
53+
extern void canonicalize_path_enc(char *path, int encoding);
5354
extern void make_native_path(char *path);
5455
extern void cleanup_path(char *path);
5556
extern bool path_contains_parent_reference(const char *path);

src/port/path.c

+91-14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <unistd.h>
3636
#endif
3737

38+
#include "mb/pg_wchar.h"
3839
#include "pg_config_paths.h"
3940

4041

@@ -44,6 +45,10 @@
4445
#define IS_PATH_VAR_SEP(ch) ((ch) == ';')
4546
#endif
4647

48+
#ifdef WIN32
49+
static void debackslash_path(char *path, int encoding);
50+
static int pg_sjis_mblen(const unsigned char *s);
51+
#endif
4752
static void make_relative_path(char *ret_path, const char *target_path,
4853
const char *bin_path, const char *my_exec_path);
4954
static void trim_directory(char *path);
@@ -147,10 +152,73 @@ last_dir_separator(const char *filename)
147152
}
148153

149154

155+
#ifdef WIN32
156+
157+
/*
158+
* Convert '\' to '/' within the given path, assuming the path
159+
* is in the specified encoding.
160+
*/
161+
static void
162+
debackslash_path(char *path, int encoding)
163+
{
164+
char *p;
165+
166+
/*
167+
* Of the supported encodings, only Shift-JIS has multibyte characters
168+
* that can include a byte equal to '\' (0x5C). So rather than implement
169+
* a fully encoding-aware conversion, we special-case SJIS. (Invoking the
170+
* general encoding-aware logic in wchar.c is impractical here for
171+
* assorted reasons.)
172+
*/
173+
if (encoding == PG_SJIS)
174+
{
175+
for (p = path; *p; p += pg_sjis_mblen((const unsigned char *) p))
176+
{
177+
if (*p == '\\')
178+
*p = '/';
179+
}
180+
}
181+
else
182+
{
183+
for (p = path; *p; p++)
184+
{
185+
if (*p == '\\')
186+
*p = '/';
187+
}
188+
}
189+
}
190+
150191
/*
151-
* make_native_path - on WIN32, change / to \ in the path
192+
* SJIS character length
152193
*
153-
* This effectively undoes canonicalize_path.
194+
* This must match the behavior of
195+
* pg_encoding_mblen_bounded(PG_SJIS, s)
196+
* In particular, unlike the version of pg_sjis_mblen in src/common/wchar.c,
197+
* do not allow caller to accidentally step past end-of-string.
198+
*/
199+
static int
200+
pg_sjis_mblen(const unsigned char *s)
201+
{
202+
int len;
203+
204+
if (*s >= 0xa1 && *s <= 0xdf)
205+
len = 1; /* 1 byte kana? */
206+
else if (IS_HIGHBIT_SET(*s) && s[1] != '\0')
207+
len = 2; /* kanji? */
208+
else
209+
len = 1; /* should be ASCII */
210+
return len;
211+
}
212+
213+
#endif /* WIN32 */
214+
215+
216+
/*
217+
* make_native_path - on WIN32, change '/' to '\' in the path
218+
*
219+
* This reverses the '\'-to-'/' transformation of debackslash_path.
220+
* We need not worry about encodings here, since '/' does not appear
221+
* as a byte of a multibyte character in any supported encoding.
154222
*
155223
* This is required because WIN32 COPY is an internal CMD.EXE
156224
* command and doesn't process forward slashes in the same way
@@ -180,13 +248,14 @@ make_native_path(char *filename)
180248
* on Windows. We need them to use filenames without spaces, for which a
181249
* short filename is the safest equivalent, eg:
182250
* C:/Progra~1/
251+
*
252+
* Presently, this is only used on paths that we can assume are in a
253+
* server-safe encoding, so there's no need for an encoding-aware variant.
183254
*/
184255
void
185256
cleanup_path(char *path)
186257
{
187258
#ifdef WIN32
188-
char *ptr;
189-
190259
/*
191260
* GetShortPathName() will fail if the path does not exist, or short names
192261
* are disabled on this file system. In both cases, we just return the
@@ -196,11 +265,8 @@ cleanup_path(char *path)
196265
GetShortPathName(path, path, MAXPGPATH - 1);
197266

198267
/* Replace '\' with '/' */
199-
for (ptr = path; *ptr; ptr++)
200-
{
201-
if (*ptr == '\\')
202-
*ptr = '/';
203-
}
268+
/* All server-safe encodings are alike here, so just use PG_SQL_ASCII */
269+
debackslash_path(path, PG_SQL_ASCII);
204270
#endif
205271
}
206272

@@ -242,16 +308,29 @@ join_path_components(char *ret_path,
242308

243309

244310
/*
311+
* canonicalize_path()
312+
*
245313
* Clean up path by:
246314
* o make Win32 path use Unix slashes
247315
* o remove trailing quote on Win32
248316
* o remove trailing slash
249317
* o remove duplicate adjacent separators
250318
* o remove trailing '.'
251319
* o process trailing '..' ourselves
320+
* Modifies path in-place.
321+
*
322+
* This comes in two variants: encoding-aware and not. The non-aware version
323+
* is only safe to use on strings that are in a server-safe encoding.
252324
*/
253325
void
254326
canonicalize_path(char *path)
327+
{
328+
/* All server-safe encodings are alike here, so just use PG_SQL_ASCII */
329+
canonicalize_path_enc(path, PG_SQL_ASCII);
330+
}
331+
332+
void
333+
canonicalize_path_enc(char *path, int encoding)
255334
{
256335
char *p,
257336
*to_p;
@@ -264,17 +343,15 @@ canonicalize_path(char *path)
264343
/*
265344
* The Windows command processor will accept suitably quoted paths with
266345
* forward slashes, but barfs badly with mixed forward and back slashes.
346+
* Hence, start by converting all back slashes to forward slashes.
267347
*/
268-
for (p = path; *p; p++)
269-
{
270-
if (*p == '\\')
271-
*p = '/';
272-
}
348+
debackslash_path(path, encoding);
273349

274350
/*
275351
* In Win32, if you do: prog.exe "a b" "\c\d\" the system will pass \c\d"
276352
* as argv[2], so trim off trailing quote.
277353
*/
354+
p = path + strlen(path);
278355
if (p > path && *(p - 1) == '"')
279356
*(p - 1) = '/';
280357
#endif

0 commit comments

Comments
 (0)