Skip to content

Commit

Permalink
Make host_args.c obey REBCHR sizing
Browse files Browse the repository at this point in the history
I'm not exactly sure how the Windows build worked when the argument
processing wasn't obeying the rules for REBCHR being a wide character.
This makes sure the comparisons being done are on the wchar_t type
in the Win32 build.

As a reminder: I think REBCHR is indicative of a design problem, so I
don't feel great about adding another OS_STRxxx wrapper.  But at least
it is "quarantined".
  • Loading branch information
hostilefork committed Jul 14, 2015
1 parent 8032116 commit 1a47ee1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
18 changes: 17 additions & 1 deletion src/core/n-strings.c
Expand Up @@ -946,6 +946,22 @@ static struct digest {
}


/***********************************************************************
**
*/ int OS_STRNCMP_(const REBCHR *lhs, const REBCHR *rhs, size_t max)
/*
** Debug-only REBCHR-checked substitute for OS_STRNCMP macro
**
***********************************************************************/
{
#ifdef OS_WIDE_CHAR
return wcsncmp(cast(const wchar_t*, lhs), cast(const wchar_t*, rhs), max);
#else
return strncmp(cast(const char*, lhs), cast (const char*, rhs), max);
#endif
}


/***********************************************************************
**
*/ size_t OS_STRLEN_(const REBCHR *str)
Expand All @@ -971,7 +987,7 @@ static struct digest {
***********************************************************************/
{
#ifdef OS_WIDE_CHAR
return wcschr(cast(const wchar_t*, str), ch);
return cast(REBCHR*, wcschr(cast(const wchar_t*, str), ch));
#else
// We have to m_cast because C++ actually has a separate overload of
// strchr which will return a const pointer if the in pointer was const
Expand Down
36 changes: 18 additions & 18 deletions src/os/host-args.c
Expand Up @@ -47,23 +47,23 @@

// REBOL Option --Words:

const struct {const char *word; const int flag;} arg_words[] = {
const struct {REBCHR *word; const int flag;} arg_words[] = {
// Keep in Alpha order!
{"args", RO_ARGS | RO_EXT},
{"boot", RO_BOOT | RO_EXT},
{"cgi", RO_CGI | RO_QUIET},
{"debug", RO_DEBUG | RO_EXT},
{"do", RO_DO | RO_EXT},
{"halt", RO_HALT},
{"help", RO_HELP},
{"import", RO_IMPORT | RO_EXT},
{"quiet", RO_QUIET},
{"script", RO_SCRIPT | RO_EXT},
{"secure", RO_SECURE | RO_EXT},
{"trace", RO_TRACE},
{"verbose", RO_VERBOSE},
{"version", RO_VERSION | RO_EXT},
{"", 0},
{OS_STR_LIT("args"), RO_ARGS | RO_EXT},
{OS_STR_LIT("boot"), RO_BOOT | RO_EXT},
{OS_STR_LIT("cgi"), RO_CGI | RO_QUIET},
{OS_STR_LIT("debug"), RO_DEBUG | RO_EXT},
{OS_STR_LIT("do"), RO_DO | RO_EXT},
{OS_STR_LIT("halt"), RO_HALT},
{OS_STR_LIT("help"), RO_HELP},
{OS_STR_LIT("import"), RO_IMPORT | RO_EXT},
{OS_STR_LIT("quiet"), RO_QUIET},
{OS_STR_LIT("script"), RO_SCRIPT | RO_EXT},
{OS_STR_LIT("secure"), RO_SECURE | RO_EXT},
{OS_STR_LIT("trace"), RO_TRACE},
{OS_STR_LIT("verbose"), RO_VERBOSE},
{OS_STR_LIT("version"), RO_VERSION | RO_EXT},
{OS_STR_LIT(""), 0},
};

// REBOL Option -Characters (in alpha sorted order):
Expand Down Expand Up @@ -100,15 +100,15 @@ const struct arg_chr arg_chars2[] = {
{
int n;
int i;
char buf[16];
REBCHR buf[16];

// Some shells will pass us the line terminator. Ignore it.
if (word[0] == '\r' || word[0] == '\n') return RO_IGNORE;

OS_STRNCPY(buf, word, 15);

for (i = 0; arg_words[i].flag; i++) {
n = strncmp(buf, arg_words[i].word, 15); // correct (bytes)
n = OS_STRNCMP(buf, arg_words[i].word, 15);
if (n < 0) break;
if (n == 0) return arg_words[i].flag;
}
Expand Down

0 comments on commit 1a47ee1

Please sign in to comment.