16
16
17
17
WCHAR* EqualOrColon (_In_ __nullterminated WCHAR* szArg)
18
18
{
19
- WCHAR* pchE = wcschr (szArg,L ' =' );
20
- WCHAR* pchC = wcschr (szArg,L ' :' );
19
+ WCHAR* pchE = wcschr (szArg,W ( ' =' ) );
20
+ WCHAR* pchC = wcschr (szArg,W ( ' :' ) );
21
21
WCHAR* ret;
22
22
if (pchE == NULL ) ret = pchC;
23
23
else if (pchC == NULL ) ret = pchE;
24
24
else ret = (pchE < pchC)? pchE : pchC;
25
25
return ret;
26
26
}
27
27
28
+ // When converting a string for number parsing it is
29
+ // possible to simply cast a WCHAR to a char with no
30
+ // loss of data.
31
+ class NarrowForNumberParsing final
32
+ {
33
+ char * _buffer;
34
+ public:
35
+ NarrowForNumberParsing (const WCHAR* str)
36
+ {
37
+ size_t len = wcslen (str);
38
+ _buffer = (char *)malloc (len + 1 );
39
+ for (size_t i = 0 ; i < len; ++i)
40
+ _buffer[i] = (char )str[i];
41
+ _buffer[len] = ' \0 ' ;
42
+ }
43
+ ~NarrowForNumberParsing ()
44
+ {
45
+ free (_buffer);
46
+ }
47
+ operator const char *() const
48
+ {
49
+ return _buffer;
50
+ }
51
+ };
52
+
28
53
static DWORD g_dwSubsystem=(DWORD)-1 ,g_dwComImageFlags=(DWORD)-1 ,g_dwFileAlignment=0 ,g_dwTestRepeat=0 ;
29
54
static ULONGLONG g_stBaseAddress=0 ;
30
55
static size_t g_stSizeOfStackReserve=0 ;
@@ -214,9 +239,9 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
214
239
for (i = 1 ; i < argc; i++)
215
240
{
216
241
#ifdef TARGET_UNIX
217
- if (argv[i][0 ] == L ' -' )
242
+ if (argv[i][0 ] == W ( ' -' ) )
218
243
#else
219
- if ((argv[i][0 ] == L ' /' ) || (argv[i][0 ] == L ' -' ))
244
+ if ((argv[i][0 ] == W ( ' /' )) || (argv[i][0 ] == W ( ' -' ) ))
220
245
#endif
221
246
{
222
247
char szOpt[3 + 1 ] = { 0 };
@@ -247,7 +272,7 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
247
272
WCHAR *pStr = EqualOrColon (argv[i]);
248
273
if (pStr != NULL )
249
274
{
250
- for (pStr++; *pStr == L ' ' ; pStr++); // skip the blanks
275
+ for (pStr++; *pStr == W ( ' ' ) ; pStr++); // skip the blanks
251
276
if (wcslen (pStr)==0 ) goto InvalidOption; // if no suboption
252
277
else
253
278
{
@@ -260,9 +285,10 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
260
285
pAsm->m_dwIncludeDebugInfo = 0x103 ;
261
286
else
262
287
{
263
- const WCHAR *pFmt =((*pStr == ' 0' )&&(*(pStr+1 ) == ' x' ))? W (" %lx" ) : W (" %ld" );
264
- if (swscanf_s (pStr,pFmt,&(pAsm->m_dwIncludeDebugInfo ))!=1 )
265
- goto InvalidOption; // bad subooption
288
+ const CHAR *pFmt =((*pStr == ' 0' )&&(*(pStr+1 ) == ' x' ))? " %x" : " %d" ;
289
+ NarrowForNumberParsing str{pStr};
290
+ if (sscanf_s (str,pFmt,&(pAsm->m_dwIncludeDebugInfo ))!=1 )
291
+ goto InvalidOption; // bad subooption
266
292
}
267
293
}
268
294
}
@@ -360,7 +386,7 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
360
386
{
361
387
WCHAR *pStr = EqualOrColon (argv[i]);
362
388
if (pStr == NULL ) goto ErrorExit;
363
- for (pStr++; *pStr == L ' ' ; pStr++); // skip the blanks
389
+ for (pStr++; *pStr == W ( ' ' ) ; pStr++); // skip the blanks
364
390
if (wcslen (pStr)==0 ) goto InvalidOption; // if no file name
365
391
pAsm->m_wzResourceFile = pStr;
366
392
}
@@ -371,23 +397,23 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
371
397
{
372
398
WCHAR *pStr = EqualOrColon (argv[i]);
373
399
if (pStr == NULL ) goto InvalidOption;
374
- for (pStr++; *pStr == L ' ' ; pStr++); // skip the blanks
400
+ for (pStr++; *pStr == W ( ' ' ) ; pStr++); // skip the blanks
375
401
if (wcslen (pStr)==0 ) goto InvalidOption; // if no file name
376
402
pAsm->m_wzKeySourceName = pStr;
377
403
}
378
404
else if (!_stricmp (szOpt, " INC" ))
379
405
{
380
406
WCHAR *pStr = EqualOrColon (argv[i]);
381
407
if (pStr == NULL ) goto InvalidOption;
382
- for (pStr++; *pStr == L ' ' ; pStr++); // skip the blanks
408
+ for (pStr++; *pStr == W ( ' ' ) ; pStr++); // skip the blanks
383
409
if (wcslen (pStr)==0 ) goto InvalidOption; // if no file name
384
410
wzIncludePath = pStr;
385
411
}
386
412
else if (!_stricmp (szOpt, " OUT" ))
387
413
{
388
414
WCHAR *pStr = EqualOrColon (argv[i]);
389
415
if (pStr == NULL ) goto InvalidOption;
390
- for (pStr++; *pStr == L ' ' ; pStr++); // skip the blanks
416
+ for (pStr++; *pStr == W ( ' ' ) ; pStr++); // skip the blanks
391
417
if (wcslen (pStr)==0 ) goto InvalidOption; // if no file name
392
418
if (wcslen (pStr) >= MAX_FILENAME_LENGTH)
393
419
{
@@ -400,19 +426,20 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
400
426
{
401
427
WCHAR *pStr = EqualOrColon (argv[i]);
402
428
if (pStr == NULL ) goto InvalidOption;
403
- for (pStr++; *pStr == L ' ' ; pStr++); // skip the blanks
429
+ for (pStr++; *pStr == W ( ' ' ) ; pStr++); // skip the blanks
404
430
if (wcslen (pStr)==0 ) goto InvalidOption; // if no version string
405
431
pAsm->m_wzMetadataVersion = pStr;
406
432
}
407
433
else if (!_stricmp (szOpt, " MSV" ))
408
434
{
409
435
WCHAR *pStr = EqualOrColon (argv[i]);
410
436
if (pStr == NULL ) goto InvalidOption;
411
- for (pStr++; *pStr == L ' ' ; pStr++); // skip the blanks
437
+ for (pStr++; *pStr == W ( ' ' ) ; pStr++); // skip the blanks
412
438
if (wcslen (pStr)==0 ) goto InvalidOption; // if no version
413
439
{
414
440
int major=-1 ,minor=-1 ;
415
- if (swscanf_s (pStr,W (" %d.%d" ),&major, &minor)==2 )
441
+ NarrowForNumberParsing str{pStr};
442
+ if (sscanf_s (str," %d.%d" ,&major, &minor)==2 )
416
443
{
417
444
if ((major >= 0 )&&(major < 0xFF ))
418
445
pAsm->m_wMSVmajor = (WORD)major;
@@ -426,18 +453,20 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
426
453
WCHAR *pStr = EqualOrColon (argv[i]);
427
454
if (pStr == NULL ) goto InvalidOption;
428
455
pStr++;
429
- const WCHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? W (" %lx" ) : W (" %ld" );
430
- if (swscanf_s (pStr,pFmt,&g_dwSubsystem)!=1 ) goto InvalidOption;
456
+ const CHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? " %x" : " %d" ;
457
+ NarrowForNumberParsing str{pStr};
458
+ if (sscanf_s (str,pFmt,&g_dwSubsystem)!=1 ) goto InvalidOption;
431
459
}
432
460
else if (!_stricmp (szOpt, " SSV" ))
433
461
{
434
462
WCHAR *pStr = EqualOrColon (argv[i]);
435
463
if (pStr == NULL ) goto InvalidOption;
436
- for (pStr++; *pStr == L ' ' ; pStr++); // skip the blanks
464
+ for (pStr++; *pStr == W ( ' ' ) ; pStr++); // skip the blanks
437
465
if (wcslen (pStr)==0 ) goto InvalidOption; // if no version
438
466
{
439
467
int major=-1 ,minor=-1 ;
440
- if (swscanf_s (pStr,W (" %d.%d" ),&major, &minor)==2 )
468
+ NarrowForNumberParsing str{pStr};
469
+ if (sscanf_s (str," %d.%d" ,&major, &minor)==2 )
441
470
{
442
471
if ((major >= 0 )&&(major < 0xFFFF ))
443
472
pAsm->m_wSSVersionMajor = (WORD)major;
@@ -452,8 +481,9 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
452
481
WCHAR *pStr = EqualOrColon (argv[i]);
453
482
if (pStr == NULL ) goto InvalidOption;
454
483
pStr++;
455
- const WCHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? W (" %lx" ) : W (" %ld" );
456
- if (swscanf_s (pStr,pFmt,&g_dwFileAlignment)!=1 ) goto InvalidOption;
484
+ const CHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? " %x" : " %d" ;
485
+ NarrowForNumberParsing str{pStr};
486
+ if (sscanf_s (str,pFmt,&g_dwFileAlignment)!=1 ) goto InvalidOption;
457
487
if ((g_dwFileAlignment & (g_dwFileAlignment-1 ))
458
488
|| (g_dwFileAlignment < 0x200 ) || (g_dwFileAlignment > 0x10000 ))
459
489
{
@@ -466,16 +496,18 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
466
496
WCHAR *pStr = EqualOrColon (argv[i]);
467
497
if (pStr == NULL ) goto InvalidOption;
468
498
pStr++;
469
- const WCHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? W (" %lx" ) : W (" %ld" );
470
- if (swscanf_s (pStr,pFmt,&g_dwComImageFlags)!=1 ) goto InvalidOption;
499
+ const CHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? " %x" : " %d" ;
500
+ NarrowForNumberParsing str{pStr};
501
+ if (sscanf_s (str,pFmt,&g_dwComImageFlags)!=1 ) goto InvalidOption;
471
502
}
472
503
else if (!_stricmp (szOpt, " BAS" ))
473
504
{
474
505
WCHAR *pStr = EqualOrColon (argv[i]);
475
506
if (pStr == NULL ) goto InvalidOption;
476
507
pStr++;
477
- const WCHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? W (" %I64x" ) : W (" %I64d" );
478
- if (swscanf_s (pStr,pFmt,&g_stBaseAddress)!=1 ) goto InvalidOption;
508
+ const CHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? " %llx" : " %lld" ;
509
+ NarrowForNumberParsing str{pStr};
510
+ if (sscanf_s (str,pFmt,&g_stBaseAddress)!=1 ) goto InvalidOption;
479
511
if (g_stBaseAddress & 0xFFFF )
480
512
{
481
513
fprintf (stderr," \n Base address must be 0x10000-aligned\n " );
@@ -487,17 +519,19 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
487
519
WCHAR *pStr = EqualOrColon (argv[i]);
488
520
if (pStr == NULL ) goto InvalidOption;
489
521
pStr++;
490
- const WCHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? W (" %lx" ) : W (" %ld" );
491
- if (swscanf_s (pStr,pFmt,&g_stSizeOfStackReserve)!=1 ) goto InvalidOption;
522
+ const CHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? " %x" : " %d" ;
523
+ NarrowForNumberParsing str{pStr};
524
+ if (sscanf_s (str,pFmt,&g_stSizeOfStackReserve)!=1 ) goto InvalidOption;
492
525
}
493
526
#ifdef _SPECIAL_INTERNAL_USE_ONLY
494
527
else if (!_stricmp (szOpt, " TES" ))
495
528
{
496
529
WCHAR *pStr = EqualOrColon (argv[i]);
497
530
if (pStr == NULL ) goto InvalidOption;
498
531
pStr++;
499
- WCHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? L" %lx" : L" %ld" ;
500
- if (swscanf_s (pStr,pFmt,&g_dwTestRepeat)!=1 ) goto InvalidOption;
532
+ const CHAR *pFmt = ((*pStr==' 0' )&&(*(pStr+1 ) == ' x' ))? " %x" : " %d" ;
533
+ NarrowForNumberParsing str{pStr};
534
+ if (sscanf_s (str,pFmt,&g_dwTestRepeat)!=1 ) goto InvalidOption;
501
535
}
502
536
#endif
503
537
else
@@ -592,7 +626,7 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
592
626
do
593
627
{
594
628
j--;
595
- if (wzOutputFilename[j] == L ' .' )
629
+ if (wzOutputFilename[j] == W ( ' .' ) )
596
630
{
597
631
wzOutputFilename[j] = 0 ;
598
632
break ;
@@ -604,7 +638,7 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
604
638
if (pAsm->m_fGeneratePDB )
605
639
{
606
640
wcscpy_s (wzPdbFilename, MAX_FILENAME_LENGTH, wzOutputFilename);
607
- WCHAR* extPos = wcsrchr (wzPdbFilename, L ' .' );
641
+ WCHAR* extPos = wcsrchr (wzPdbFilename, W ( ' .' ) );
608
642
if (extPos != NULL )
609
643
*extPos = 0 ;
610
644
wcscat_s (wzPdbFilename, MAX_FILENAME_LENGTH, W (" .pdb" ));
@@ -805,11 +839,11 @@ extern "C" int _cdecl wmain(int argc, _In_ WCHAR **argv)
805
839
if (exitval || !bGeneratePdb)
806
840
{
807
841
// PE file was not created, or no debug info required. Kill PDB if any
808
- WCHAR* pc = wcsrchr (wzOutputFilename,L ' .' );
842
+ WCHAR* pc = wcsrchr (wzOutputFilename,W ( ' .' ) );
809
843
if (pc==NULL )
810
844
{
811
845
pc = &wzOutputFilename[wcslen (wzOutputFilename)];
812
- *pc = L ' .' ;
846
+ *pc = W ( ' .' ) ;
813
847
}
814
848
wcscpy_s (pc+1 ,4 ,W (" PDB" ));
815
849
#undef DeleteFileW
0 commit comments