Skip to content

Commit 9640b54

Browse files
authored
Merge pull request #2857 from martinhsv/v2/master
Fix: FILES_TMP_CONTENT may sometimes lack complete content
2 parents afb48b2 + 4324f0a commit 9640b54

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

Diff for: CHANGES

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
DD mmm YYYY - 2.9.x (to be released)
22
-------------------
33

4+
* Fix: FILES_TMP_CONTENT may sometimes lack complete content
5+
[Issue #2857 - gieltje, @airween, @dune73, @martinhsv]
46
* Support configurable limit on number of arguments processed
57
[Issue #2844 - @jleproust, @martinhsv]
68
* Silence compiler warning about discarded const

Diff for: apache2/re_variables.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ static int var_files_tmp_contents_generate(modsec_rec *msr, msre_var *var,
11731173
FILE *file;
11741174
size_t nread;
11751175
char *full_content = NULL;
1176+
char *full_content_tmp_ptr = NULL;
11761177
size_t total_lenght = 0;
11771178
msre_var *rvar = NULL;
11781179

@@ -1182,19 +1183,23 @@ static int var_files_tmp_contents_generate(modsec_rec *msr, msre_var *var,
11821183
continue;
11831184
}
11841185

1186+
full_content = (char *)apr_pcalloc(mptmp, (sizeof(char)*parts[i]->length) + 1);
1187+
if (full_content == NULL) {
1188+
if (msr->txcfg->debuglog_level >= 3) {
1189+
msr_log(msr, 3, "Variable FILES_TMP_CONTENT will not be created, not " \
1190+
"enough memory available.");
1191+
}
1192+
goto files_tmp_content_not_enough_mem;
1193+
}
1194+
full_content_tmp_ptr = full_content;
1195+
11851196
while ((nread = fread(buf, 1, 1023, file)) > 0)
11861197
{
1187-
total_lenght += nread;
1188-
buf[nread] = '\0';
1189-
if (full_content == NULL)
1190-
{
1191-
full_content = apr_psprintf(mptmp, "%s", buf);
1192-
}
1193-
else
1194-
{
1195-
full_content = apr_psprintf(mptmp, "%s%s", full_content, buf);
1196-
}
1198+
full_content_tmp_ptr = memcpy(full_content_tmp_ptr, buf, nread);
1199+
full_content_tmp_ptr += nread;
1200+
total_lenght += nread;
11971201
}
1202+
full_content_tmp_ptr[total_lenght] = '\0';
11981203
fclose(file);
11991204

12001205
rvar = apr_pmemdup(mptmp, var, sizeof(msre_var));
@@ -1209,6 +1214,7 @@ static int var_files_tmp_contents_generate(modsec_rec *msr, msre_var *var,
12091214
}
12101215
}
12111216

1217+
files_tmp_content_not_enough_mem:
12121218
return count;
12131219
}
12141220

0 commit comments

Comments
 (0)