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

Commit54f9afe

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 wasdoing that in a non-encoding-aware fashion; but in SJIS thereare valid two-byte characters whose second byte matches '\'.So encoding corruption ensues if such a character is used inthe path.We can fairly easily fix this if we know which encoding isin use, but a lot of our utilities don't have much of a clueabout that. After some discussion we decided we'd settle forfixing this only in psql, and assuming that its value ofclient_encoding matches what the user is typing.It seems hopeless to get the server to deal with the problematiccharacters in database path names, so we'll just declare thatcase to be unsupported. That means nothing need be done inthe server, nor in utility programs whose only contact withfile path names is for database paths. But psql frequentlydeals with client-side file paths, so it'd be good if itdidn't mess those up.Bug: #18735Reported-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.orgBackpatch-through: 13
1 parent25e9948 commit54f9afe

File tree

4 files changed

+97
-19
lines changed

4 files changed

+97
-19
lines changed

‎src/bin/psql/command.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch,
10521052
expand_tilde(&fname);
10531053
if (fname)
10541054
{
1055-
canonicalize_path(fname);
1055+
canonicalize_path_enc(fname,pset.encoding);
10561056
/* Always clear buffer if the file isn't modified */
10571057
discard_on_quit= true;
10581058
}
@@ -2622,7 +2622,7 @@ exec_command_write(PsqlScanState scan_state, bool active_branch,
26222622
}
26232623
else
26242624
{
2625-
canonicalize_path(fname);
2625+
canonicalize_path_enc(fname,pset.encoding);
26262626
fd=fopen(fname,"w");
26272627
}
26282628
if (!fd)
@@ -4010,7 +4010,7 @@ process_file(char *filename, bool use_relative_path)
40104010
}
40114011
elseif (strcmp(filename,"-")!=0)
40124012
{
4013-
canonicalize_path(filename);
4013+
canonicalize_path_enc(filename,pset.encoding);
40144014

40154015
/*
40164016
* If we were asked to resolve the pathname relative to the location
@@ -4024,7 +4024,7 @@ process_file(char *filename, bool use_relative_path)
40244024
strlcpy(relpath,pset.inputfile,sizeof(relpath));
40254025
get_parent_directory(relpath);
40264026
join_path_components(relpath,relpath,filename);
4027-
canonicalize_path(relpath);
4027+
canonicalize_path_enc(relpath,pset.encoding);
40284028

40294029
filename=relpath;
40304030
}

‎src/bin/psql/copy.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern char *first_path_var_separator(const char *pathlist);
5050
externvoidjoin_path_components(char*ret_path,
5151
constchar*head,constchar*tail);
5252
externvoidcanonicalize_path(char*path);
53+
externvoidcanonicalize_path_enc(char*path,intencoding);
5354
externvoidmake_native_path(char*path);
5455
externvoidcleanup_path(char*path);
5556
externboolpath_contains_parent_reference(constchar*path);

‎src/port/path.c

Lines changed: 91 additions & 14 deletions
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
#defineIS_PATH_VAR_SEP(ch) ((ch) == ';')
4546
#endif
4647

48+
#ifdefWIN32
49+
staticvoiddebackslash_path(char*path,intencoding);
50+
staticintpg_sjis_mblen(constunsignedchar*s);
51+
#endif
4752
staticvoidmake_relative_path(char*ret_path,constchar*target_path,
4853
constchar*bin_path,constchar*my_exec_path);
4954
staticvoidtrim_directory(char*path);
@@ -147,10 +152,73 @@ last_dir_separator(const char *filename)
147152
}
148153

149154

155+
#ifdefWIN32
156+
157+
/*
158+
* Convert '\' to '/' within the given path, assuming the path
159+
* is in the specified encoding.
160+
*/
161+
staticvoid
162+
debackslash_path(char*path,intencoding)
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((constunsignedchar*)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+
staticint
200+
pg_sjis_mblen(constunsignedchar*s)
201+
{
202+
intlen;
203+
204+
if (*s >=0xa1&&*s <=0xdf)
205+
len=1;/* 1 byte kana? */
206+
elseif (IS_HIGHBIT_SET(*s)&&s[1]!='\0')
207+
len=2;/* kanji? */
208+
else
209+
len=1;/* should be ASCII */
210+
returnlen;
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
#ifdefWIN32
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,intencoding)
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)

[8]ページ先頭

©2009-2025 Movatter.jp