Commit 52e92fee
52e92fee3c39Merged
ydah <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
fix(runtime): quote Windows system args
Details
- Commit
- 52e92fee3c39
- Parent
- 466115d356b6
lib/sp_runtime.hmodifiedView file
@@ -1751,19 +1751,126 @@
17511751
17521752
/* System/backtick support */17531753
static int sp_last_status = 0;1754+
#ifdef _WIN321755+
static char *sp_win_quote_arg(const char *arg) {1756+
const char *p = arg;1757+
size_t len = 2;1758+
mrb_bool quote = (*p == '\0') ? TRUE : FALSE;1759+
while (*p) {1760+
if (isspace((unsigned char)*p) || *p == '"') quote = TRUE;1761+
if (*p == '"') {1762+
len += 2;1763+
}1764+
else {1765+
len += 1;1766+
}1767+
p++;1768+
}1769+
if (!quote) {1770+
char *copy = (char *)malloc(len - 1);1771+
if (copy) memcpy(copy, arg, len - 1);1772+
return copy;1773+
}1774+
1775+
p = arg;1776+
size_t bs = 0;1777+
len = 3;1778+
while (*p) {1779+
if (*p == '\\') {1780+
bs++;1781+
}1782+
else if (*p == '"') {1783+
len += bs * 2 + 2;1784+
bs = 0;1785+
}1786+
else {1787+
len += bs + 1;1788+
bs = 0;1789+
}1790+
p++;1791+
}1792+
len += bs * 2;1793+
1794+
char *out = (char *)malloc(len);1795+
if (!out) return NULL;1796+
char *q = out;1797+
*q++ = '"';1798+
p = arg;1799+
bs = 0;1800+
while (*p) {1801+
if (*p == '\\') {1802+
bs++;1803+
}1804+
else if (*p == '"') {1805+
while (bs > 0) {1806+
*q++ = '\\';1807+
*q++ = '\\';1808+
bs--;1809+
}1810+
*q++ = '\\';1811+
*q++ = '"';1812+
bs = 0;1813+
}1814+
else {1815+
while (bs > 0) {1816+
*q++ = '\\';1817+
bs--;1818+
}1819+
*q++ = *p;1820+
bs = 0;1821+
}1822+
p++;1823+
}1824+
while (bs > 0) {1825+
*q++ = '\\';1826+
*q++ = '\\';1827+
bs--;1828+
}1829+
*q++ = '"';1830+
*q = '\0';1831+
return out;1832+
}1833+
#endif1834+
17541835
static mrb_bool sp_system_args(int argc, const char *const *argv) {17551836
if (argc <= 0 || argv == NULL || argv[0] == NULL) {17561837
sp_last_status = -1;17571838
return FALSE;17581839
}1759-
fflush(stdout);1840+
fflush(NULL);17601841
#ifdef _WIN321761-
intptr_t rc = _spawnvp(_P_WAIT, argv[0], argv);1842+
char **quoted_argv = (char **)malloc(sizeof(char *) * (size_t)(argc + 1));1843+
if (!quoted_argv) {1844+
sp_last_status = -1;1845+
return FALSE;1846+
}1847+
int i = 0;1848+
while (i < argc) {1849+
if (argv[i] == NULL) {1850+
while (i-- > 0) free(quoted_argv[i]);1851+
free(quoted_argv);1852+
sp_last_status = -1;1853+
return FALSE;1854+
}1855+
quoted_argv[i] = sp_win_quote_arg(argv[i]);1856+
if (!quoted_argv[i]) {1857+
while (i-- > 0) free(quoted_argv[i]);1858+
free(quoted_argv);1859+
sp_last_status = -1;1860+
return FALSE;1861+
}1862+
i++;1863+
}1864+
quoted_argv[argc] = NULL;1865+
1866+
intptr_t rc = _spawnvp(_P_WAIT, argv[0], (const char *const *)quoted_argv);1867+
for (i = 0; i < argc; i++) free(quoted_argv[i]);1868+
free(quoted_argv);17621869
if (rc < 0) {17631870
sp_last_status = -1;17641871
return FALSE;17651872
}1766-
sp_last_status = (int)rc;1873+
sp_last_status = (int)rc << 8;17671874
return rc == 0 ? TRUE : FALSE;17681875
#else17691876
pid_t pid = fork();spinel_codegen.rbmodifiedView file
Loading diff…