Reference re-parser: the subset of CommandLineToArgvW our builder emits (every * arg quote-wrapped; \" for embedded quotes; backslashes doubled before a quote). */
| 1474 | /* Reference re-parser: the subset of CommandLineToArgvW our builder emits (every |
| 1475 | * arg quote-wrapped; \" for embedded quotes; backslashes doubled before a quote). */ |
| 1476 | static int parse_win_cmdline(const char *cmd, char out[][256], int max_args) { |
| 1477 | int argc = 0; |
| 1478 | const char *p = cmd; |
| 1479 | while (*p) { |
| 1480 | while (*p == ' ' || *p == '\t') { |
| 1481 | p++; |
| 1482 | } |
| 1483 | if (!*p || argc >= max_args) { |
| 1484 | break; |
| 1485 | } |
| 1486 | char *o = out[argc]; |
| 1487 | size_t oi = 0; |
| 1488 | bool in_quotes = false; |
| 1489 | /* Guard every write: each out[] row is 256 bytes; test args stay well under |
| 1490 | * that, but cap defensively so a future longer arg fails a length assertion |
| 1491 | * rather than smashing the stack. */ |
| 1492 | #define PUTO(ch) \ |
| 1493 | do { \ |
| 1494 | if (oi < 255) { \ |
| 1495 | o[oi++] = (ch); \ |
| 1496 | } \ |
| 1497 | } while (0) |
| 1498 | for (;;) { |
| 1499 | size_t nbs = 0; |
| 1500 | while (*p == '\\') { |
| 1501 | nbs++; |
| 1502 | p++; |
| 1503 | } |
| 1504 | if (*p == '"') { |
| 1505 | for (size_t k = 0; k < nbs / 2; k++) { |
| 1506 | PUTO('\\'); |
| 1507 | } |
| 1508 | if (nbs % 2) { |
| 1509 | PUTO('"'); /* odd run → the quote is an escaped literal */ |
| 1510 | } else { |
| 1511 | in_quotes = !in_quotes; /* even run → the quote is a delimiter */ |
| 1512 | } |
| 1513 | p++; |
| 1514 | } else { |
| 1515 | for (size_t k = 0; k < nbs; k++) { |
| 1516 | PUTO('\\'); |
| 1517 | } |
| 1518 | if (*p == '\0' || (!in_quotes && (*p == ' ' || *p == '\t'))) { |
| 1519 | break; |
| 1520 | } |
| 1521 | PUTO(*p); |
| 1522 | p++; |
| 1523 | } |
| 1524 | } |
| 1525 | #undef PUTO |
| 1526 | o[oi] = '\0'; |
| 1527 | argc++; |
| 1528 | } |
| 1529 | return argc; |
| 1530 | } |
| 1531 | |
| 1532 | static bool cmdline_roundtrips(const char *const *argv) { |
| 1533 | char cmd[4096]; |
no outgoing calls
no test coverage detected