Fossil SCM

Fix related to previous one: prevent to convert from UTF-8 to Unicode before writing to the (non-redirected) windows console. Strange effects could happen.

jan.nijtmans 2014-11-26 14:53 trunk
Commit 156ef9ec06a069f3791c4fba8e3972a1099bad7b
1 file changed +11 -2
+11 -2
--- src/utf8.c
+++ src/utf8.c
@@ -309,10 +309,11 @@
309309
int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){
310310
#ifdef _WIN32
311311
int nChar, written = 0;
312312
wchar_t *zUnicode; /* Unicode version of zUtf8 */
313313
DWORD dummy;
314
+ Blob blob;
314315
315316
static int istty[2] = { -1, -1 };
316317
if( istty[toStdErr] == -1 ){
317318
istty[toStdErr] = _isatty(toStdErr + 1) != 0;
318319
}
@@ -319,16 +320,24 @@
319320
if( !istty[toStdErr] ){
320321
/* stdout/stderr is not a console. */
321322
return -1;
322323
}
323324
324
- nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, NULL, 0);
325
+ /* If blob to be written to the Windows console is not
326
+ * UTF-8, convert it to UTF-8 first.
327
+ */
328
+ blob_init(&blob, zUtf8, nByte);
329
+ blob_to_utf8_no_bom(&blob, 1);
330
+ nChar = MultiByteToWideChar(CP_UTF8, 0, blob_buffer(&blob),
331
+ blob_size(&blob), NULL, 0);
325332
zUnicode = malloc( (nChar + 1) *sizeof(zUnicode[0]) );
326333
if( zUnicode==0 ){
327334
return 0;
328335
}
329
- nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zUnicode, nChar);
336
+ nChar = MultiByteToWideChar(CP_UTF8, 0, blob_buffer(&blob),
337
+ blob_size(&blob), zUnicode, nChar);
338
+ blob_reset(&blob);
330339
/* Split WriteConsoleW call into multiple chunks, if necessary. See:
331340
* <https://connect.microsoft.com/VisualStudio/feedback/details/635230> */
332341
while( written < nChar ){
333342
int size = nChar-written;
334343
if( size > 26000 ) size = 26000;
335344
--- src/utf8.c
+++ src/utf8.c
@@ -309,10 +309,11 @@
309 int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){
310 #ifdef _WIN32
311 int nChar, written = 0;
312 wchar_t *zUnicode; /* Unicode version of zUtf8 */
313 DWORD dummy;
 
314
315 static int istty[2] = { -1, -1 };
316 if( istty[toStdErr] == -1 ){
317 istty[toStdErr] = _isatty(toStdErr + 1) != 0;
318 }
@@ -319,16 +320,24 @@
319 if( !istty[toStdErr] ){
320 /* stdout/stderr is not a console. */
321 return -1;
322 }
323
324 nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, NULL, 0);
 
 
 
 
 
 
325 zUnicode = malloc( (nChar + 1) *sizeof(zUnicode[0]) );
326 if( zUnicode==0 ){
327 return 0;
328 }
329 nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zUnicode, nChar);
 
 
330 /* Split WriteConsoleW call into multiple chunks, if necessary. See:
331 * <https://connect.microsoft.com/VisualStudio/feedback/details/635230> */
332 while( written < nChar ){
333 int size = nChar-written;
334 if( size > 26000 ) size = 26000;
335
--- src/utf8.c
+++ src/utf8.c
@@ -309,10 +309,11 @@
309 int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){
310 #ifdef _WIN32
311 int nChar, written = 0;
312 wchar_t *zUnicode; /* Unicode version of zUtf8 */
313 DWORD dummy;
314 Blob blob;
315
316 static int istty[2] = { -1, -1 };
317 if( istty[toStdErr] == -1 ){
318 istty[toStdErr] = _isatty(toStdErr + 1) != 0;
319 }
@@ -319,16 +320,24 @@
320 if( !istty[toStdErr] ){
321 /* stdout/stderr is not a console. */
322 return -1;
323 }
324
325 /* If blob to be written to the Windows console is not
326 * UTF-8, convert it to UTF-8 first.
327 */
328 blob_init(&blob, zUtf8, nByte);
329 blob_to_utf8_no_bom(&blob, 1);
330 nChar = MultiByteToWideChar(CP_UTF8, 0, blob_buffer(&blob),
331 blob_size(&blob), NULL, 0);
332 zUnicode = malloc( (nChar + 1) *sizeof(zUnicode[0]) );
333 if( zUnicode==0 ){
334 return 0;
335 }
336 nChar = MultiByteToWideChar(CP_UTF8, 0, blob_buffer(&blob),
337 blob_size(&blob), zUnicode, nChar);
338 blob_reset(&blob);
339 /* Split WriteConsoleW call into multiple chunks, if necessary. See:
340 * <https://connect.microsoft.com/VisualStudio/feedback/details/635230> */
341 while( written < nChar ){
342 int size = nChar-written;
343 if( size > 26000 ) size = 26000;
344

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button