Skip to content

Commit

Permalink
Translate strings to native encoding defensively before use.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@85695 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
kalibera committed Dec 18, 2023
1 parent 6fd9fb5 commit e93f59b
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/modules/internet/libcurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,14 @@ static void curlCommon(CURL *hnd, int redirect, int verify)
int Default = 1;
SEXP sua = GetOption1(install("HTTPUserAgent")); // set in utils startup
if (TYPEOF(sua) == STRSXP && LENGTH(sua) == 1 ) {
const char *p = CHAR(STRING_ELT(sua, 0));
const void *vmax = vmaxget();
const char *p = translateChar(STRING_ELT(sua, 0));
if (p[0] && p[1] && p[2] && p[0] == 'R' && p[1] == ' ' && p[2] == '(') {
} else {
Default = 0;
curl_easy_setopt(hnd, CURLOPT_USERAGENT, p);
}
vmaxset(vmax);
}
if (Default) {
char buf[20];
Expand Down Expand Up @@ -533,6 +535,7 @@ in_do_curlDownload(SEXP call, SEXP op, SEXP args, SEXP rho)
const char *url, *file, *mode;
int quiet, cacheOK;
struct curl_slist *headers = NULL;
const void *vmax = vmaxget();

scmd = CAR(args); args = CDR(args);
if (!isString(scmd) || length(scmd) < 1)
Expand All @@ -553,7 +556,7 @@ in_do_curlDownload(SEXP call, SEXP op, SEXP args, SEXP rho)
smode = CAR(args); args = CDR(args);
if (!isString(smode) || length(smode) != 1)
error(_("invalid '%s' argument"), "mode");
mode = CHAR(STRING_ELT(smode, 0));
mode = translateChar(STRING_ELT(smode, 0));
cacheOK = asLogical(CAR(args)); args = CDR(args);
if (cacheOK == NA_LOGICAL)
error(_("invalid '%s' argument"), "cacheOK");
Expand All @@ -563,7 +566,8 @@ in_do_curlDownload(SEXP call, SEXP op, SEXP args, SEXP rho)
if(TYPEOF(sheaders) != NILSXP) {
for (int i = 0; i < LENGTH(sheaders); i++) {
struct curl_slist *tmp =
curl_slist_append(headers, CHAR(STRING_ELT(sheaders, i)));
curl_slist_append(headers,
translateChar(STRING_ELT(sheaders, i)));
if (!tmp) {
if (headers) curl_slist_free_all(headers);
error(_("out of memory"));
Expand Down Expand Up @@ -604,7 +608,7 @@ in_do_curlDownload(SEXP call, SEXP op, SEXP args, SEXP rho)
}

for(int i = 0; i < nurls; i++) {
url = CHAR(STRING_ELT(scmd, i));
url = translateChar(STRING_ELT(scmd, i));
hnd[i] = curl_easy_init();
if (!hnd[i]) {
n_err += 1;
Expand Down Expand Up @@ -695,6 +699,7 @@ in_do_curlDownload(SEXP call, SEXP op, SEXP args, SEXP rho)
if (n_err == nurls) {
// no dest files could be opened, so bail out
curl_multi_cleanup(mhnd);
vmaxset(vmax);
return ScalarInteger(1);
}

Expand Down Expand Up @@ -791,11 +796,13 @@ in_do_curlDownload(SEXP call, SEXP op, SEXP args, SEXP rho)
else if (n_err) warning(_("some files were not downloaded"));
} else if(n_err) {
if (status != 200)
error(_("cannot open URL '%s'"), CHAR(STRING_ELT(scmd, 0)));
error(_("cannot open URL '%s'"),
translateChar(STRING_ELT(scmd, 0)));
else
error(_("download from '%s' failed"), CHAR(STRING_ELT(scmd, 0)));
error(_("download from '%s' failed"),
translateChar(STRING_ELT(scmd, 0)));
}

vmaxset(vmax);
return ScalarInteger(0);
#endif
}
Expand Down Expand Up @@ -1052,9 +1059,11 @@ in_newCurlUrl(const char *description, const char * const mode,
/* for Solaris 12.5 */ new = NULL;
}
ctxt->headers = NULL;
const void *vmax = vmaxget();
for (int i = 0; i < LENGTH(headers); i++) {
struct curl_slist *tmp =
curl_slist_append(ctxt->headers, CHAR(STRING_ELT(headers, i)));
curl_slist_append(ctxt->headers,
translateChar(STRING_ELT(headers, i)));
if (!tmp) {
free(new->description); free(new->class); free(new->private);
free(new); curl_slist_free_all(ctxt->headers);
Expand All @@ -1063,6 +1072,7 @@ in_newCurlUrl(const char *description, const char * const mode,
}
ctxt->headers = tmp;
}
vmaxset(vmax);
return new;
#else
error(_("url(method = \"libcurl\") is not supported on this platform"));
Expand Down

0 comments on commit e93f59b

Please sign in to comment.