VirtualBox

Changeset 2781 in kBuild for trunk/src/kash


Ignore:
Timestamp:
May 9, 2015 6:52:15 PM (10 years ago)
Author:
bird
Message:

kmk_ash: Fixed argument quoting on windows.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/shinstance.c

    r2652 r2781  
    11351135        *p = '\0';
    11361136
    1137         /* Create the command line. */
     1137        /* Figure the size of the command line. Double quotes makes this
     1138           tedious and we overestimate to simplify. */
    11381139        cmdline_size = 2;
    11391140        for (i = 0; argv[i]; i++)
    1140             cmdline_size += strlen(argv[i]) + 3;
     1141        {
     1142            const char *arg = argv[i];
     1143            cmdline_size += strlen(arg) + 3;
     1144            arg = strchr(arg, '"');
     1145            if (arg)
     1146            {
     1147                do
     1148                    cmdline_size++;
     1149                while ((arg = strchr(arg + 1, '"')) != NULL);
     1150                arg = argv[i] - 1;
     1151                while ((arg = strchr(arg + 1, '\\')) != NULL);
     1152                    cmdline_size++;
     1153            }
     1154        }
     1155
     1156        /* Create the command line. */
    11411157        cmdline = p = sh_malloc(psh, cmdline_size);
    11421158        for (i = 0; argv[i]; i++)
    11431159        {
    1144             size_t len = strlen(argv[i]);
    1145             int quoted = !!strpbrk(argv[i], " \t"); /** @todo Do this quoting business right. */
     1160            const char *arg = argv[i];
     1161            const char *cur = arg;
     1162            size_t len = strlen(arg);
     1163            int quoted = 0;
     1164            char ch;
     1165            while ((ch = *cur++) != '\0')
     1166                if (ch <= 0x20 || strchr("&><|%", ch) != NULL)
     1167                {
     1168                    quoted = 1;
     1169                    break;
     1170                }
     1171
    11461172            if (i != 0)
    11471173                *(p++) = ' ';
    11481174            if (quoted)
    11491175                *(p++) = '"';
    1150             memcpy(p, argv[i], len);
    1151             p += len;
     1176            if (memchr(arg, '"', len) == NULL)
     1177            {
     1178                memcpy(p, arg, len);
     1179                p += len;
     1180            }
     1181            else
     1182            {   /* MS CRT style: double quotes must be escaped; backslashes
     1183                   must be escaped if followed by double quotes. */
     1184                while ((ch = *arg++) != '\0')
     1185                    if (ch != '\\' && ch != '"')
     1186                        *p++ = ch;
     1187                    else if (ch == '"')
     1188                    {
     1189                        *p++ = '\\';
     1190                        *p++ = '"';
     1191                    }
     1192                    else
     1193                    {
     1194                        unsigned slashes = 1;
     1195                        *p++ = '\\';
     1196                        while (*arg == '\\')
     1197                        {
     1198                            *p++ = '\\';
     1199                            slashes++;
     1200                            arg++;
     1201                        }
     1202                        if (*arg == '"')
     1203                        {
     1204                            while (slashes-- > 0)
     1205                                *p++ = '\\';
     1206                            *p++ = '\\';
     1207                            *p++ = '"';
     1208                            arg++;
     1209                        }
     1210                    }
     1211            }
    11521212            if (quoted)
    11531213                *(p++) = '"';
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette