summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorivan <ivan@13f79535-47bb-0310-9956-ffa450edef68>2022-06-30 16:58:11 +0000
committerivan <ivan@13f79535-47bb-0310-9956-ffa450edef68>2022-06-30 16:58:11 +0000
commit6f3a79699e51bce0628c7d924f3a93d319c418fa (patch)
tree4939e5a9f0b58c938fae96023f74d4080eb1da4d
parenta2a73400c168ddc0fcbcd7c6b14ab415220674e3 (diff)
downloadlibapr-6f3a79699e51bce0628c7d924f3a93d319c418fa.tar.gz
On 1.8.x branch: Merge r1808456 from trunk:
Win32: Don't seek to the end when opening files with APR_FOPEN_APPEND. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/branches/1.8.x@1902377 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--CHANGES3
-rw-r--r--file_io/win32/open.c1
-rw-r--r--test/testfile.c51
3 files changed, 54 insertions, 1 deletions
diff --git a/CHANGES b/CHANGES
index 1e614b628..675365611 100644
--- a/CHANGES
+++ b/CHANGES
@@ -20,6 +20,9 @@ Changes for APR 1.8.0
*) apr_file_gets: Optimize for buffered files on Windows.
[Evgeny Kotkov <evgeny.kotkov visualsvn.com>]
+ *) Don't seek to the end when opening files with APR_FOPEN_APPEND on Windows.
+ [Evgeny Kotkov <evgeny.kotkov visualsvn.com>]
+
Changes for APR 1.7.1
*) SECURITY: CVE-2021-35940 (cve.mitre.org)
diff --git a/file_io/win32/open.c b/file_io/win32/open.c
index 3c00bfc79..f1a46286d 100644
--- a/file_io/win32/open.c
+++ b/file_io/win32/open.c
@@ -445,7 +445,6 @@ APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new, const char *fname,
if (flag & APR_FOPEN_APPEND) {
(*new)->append = 1;
- SetFilePointer((*new)->filehand, 0, NULL, FILE_END);
}
if (flag & APR_FOPEN_BUFFERED) {
(*new)->buffered = 1;
diff --git a/test/testfile.c b/test/testfile.c
index 9419b0a7c..9087b04f8 100644
--- a/test/testfile.c
+++ b/test/testfile.c
@@ -1711,6 +1711,56 @@ static void test_append_locked(abts_case *tc, void *data)
apr_file_remove(fname, p);
}
+static void test_append_read(abts_case *tc, void *data)
+{
+ apr_status_t rv;
+ apr_file_t *f;
+ const char *fname = "data/testappend_read.dat";
+ apr_off_t offset;
+ char buf[64];
+
+ apr_file_remove(fname, p);
+
+ /* Create file with contents. */
+ rv = apr_file_open(&f, fname, APR_FOPEN_WRITE | APR_FOPEN_CREATE,
+ APR_FPROT_OS_DEFAULT, p);
+ APR_ASSERT_SUCCESS(tc, "create file", rv);
+
+ rv = apr_file_puts("abc", f);
+ APR_ASSERT_SUCCESS(tc, "write to file", rv);
+ apr_file_close(f);
+
+ /* Re-open it with APR_FOPEN_APPEND. */
+ rv = apr_file_open(&f, fname,
+ APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_APPEND,
+ APR_FPROT_OS_DEFAULT, p);
+ APR_ASSERT_SUCCESS(tc, "open file", rv);
+
+ /* Test the initial file offset. Even though we used APR_FOPEN_APPEND,
+ * the offset should be kept in the beginning of the file until the
+ * first append. (Previously, the Windows implementation performed
+ * an erroneous seek to the file's end right after opening it.)
+ */
+ offset = 0;
+ rv = apr_file_seek(f, APR_CUR, &offset);
+ APR_ASSERT_SUCCESS(tc, "get file offset", rv);
+ ABTS_INT_EQUAL(tc, 0, (int)offset);
+
+ /* Test reading from the file. */
+ rv = apr_file_gets(buf, sizeof(buf), f);
+ APR_ASSERT_SUCCESS(tc, "read from file", rv);
+ ABTS_STR_EQUAL(tc, "abc", buf);
+
+ /* Test the file offset after reading. */
+ offset = 0;
+ rv = apr_file_seek(f, APR_CUR, &offset);
+ APR_ASSERT_SUCCESS(tc, "get file offset", rv);
+ ABTS_INT_EQUAL(tc, 3, (int)offset);
+
+ apr_file_close(f);
+ apr_file_remove(fname, p);
+}
+
abts_suite *testfile(abts_suite *suite)
{
suite = ADD_SUITE(suite)
@@ -1765,6 +1815,7 @@ abts_suite *testfile(abts_suite *suite)
abts_run_test(suite, test_datasync_on_stream, NULL);
abts_run_test(suite, test_atomic_append, NULL);
abts_run_test(suite, test_append_locked, NULL);
+ abts_run_test(suite, test_append_read, NULL);
return suite;
}