aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Albert <danalbert@google.com>2015-05-19 18:17:31 -0700
committerDan Albert <danalbert@google.com>2015-06-01 11:28:13 -0700
commit5f3e19dbbeb851fff5cf7f9869d344feca479413 (patch)
treef83802769593c7d7a534d51ad3dcfddb71557d40
parentca10ac6dd45f73752e8822fee606d83116a5721a (diff)
downloadandroid_bionic-5f3e19dbbeb851fff5cf7f9869d344feca479413.tar.gz
android_bionic-5f3e19dbbeb851fff5cf7f9869d344feca479413.tar.bz2
android_bionic-5f3e19dbbeb851fff5cf7f9869d344feca479413.zip
Fix error handling for negative size in ftruncate.
Bug: 21309901 Change-Id: I54692ab8105dd09db6af7a2c0894a17bdd118aa0 (cherry picked from commit c05554ec5c9aff5e0f1e83de9bb62c3569eecca2)
-rw-r--r--libc/Android.mk1
-rw-r--r--libc/SYSCALLS.TXT1
-rw-r--r--libc/arch-arm/syscalls/ftruncate.S14
-rw-r--r--libc/arch-mips/syscalls/ftruncate.S19
-rw-r--r--libc/arch-x86/syscalls/ftruncate.S26
-rw-r--r--libc/bionic/ftruncate.cpp28
-rw-r--r--tests/unistd_test.cpp9
7 files changed, 38 insertions, 60 deletions
diff --git a/libc/Android.mk b/libc/Android.mk
index f09733e65..d6274dd82 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -131,6 +131,7 @@ libc_bionic_ndk_src_files := \
bionic/ffs.cpp \
bionic/flockfile.cpp \
bionic/fpclassify.cpp \
+ bionic/ftruncate.cpp \
bionic/futimens.cpp \
bionic/getcwd.cpp \
bionic/gethostname.cpp \
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 02468335f..9d7f839c6 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -151,7 +151,6 @@ int utimensat(int, const char*, const struct timespec times[2], int) all
off_t lseek(int, off_t, int) arm,mips,x86
int __llseek:_llseek(int, unsigned long, unsigned long, off64_t*, int) arm,mips,x86
off_t lseek|lseek64(int, off_t, int) arm64,mips64,x86_64
-int ftruncate(int, off_t) arm,mips,x86
int ftruncate64(int, off64_t) arm,mips,x86
int ftruncate|ftruncate64(int, off_t) arm64,mips64,x86_64
ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count) arm,mips,x86
diff --git a/libc/arch-arm/syscalls/ftruncate.S b/libc/arch-arm/syscalls/ftruncate.S
deleted file mode 100644
index 1bfe2f39c..000000000
--- a/libc/arch-arm/syscalls/ftruncate.S
+++ /dev/null
@@ -1,14 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(ftruncate)
- mov ip, r7
- ldr r7, =__NR_ftruncate
- swi #0
- mov r7, ip
- cmn r0, #(MAX_ERRNO + 1)
- bxls lr
- neg r0, r0
- b __set_errno_internal
-END(ftruncate)
diff --git a/libc/arch-mips/syscalls/ftruncate.S b/libc/arch-mips/syscalls/ftruncate.S
deleted file mode 100644
index 0589c817a..000000000
--- a/libc/arch-mips/syscalls/ftruncate.S
+++ /dev/null
@@ -1,19 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(ftruncate)
- .set noreorder
- .cpload t9
- li v0, __NR_ftruncate
- syscall
- bnez a3, 1f
- move a0, v0
- j ra
- nop
-1:
- la t9,__set_errno_internal
- j t9
- nop
- .set reorder
-END(ftruncate)
diff --git a/libc/arch-x86/syscalls/ftruncate.S b/libc/arch-x86/syscalls/ftruncate.S
deleted file mode 100644
index 78d1e184f..000000000
--- a/libc/arch-x86/syscalls/ftruncate.S
+++ /dev/null
@@ -1,26 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(ftruncate)
- pushl %ebx
- .cfi_def_cfa_offset 8
- .cfi_rel_offset ebx, 0
- pushl %ecx
- .cfi_adjust_cfa_offset 4
- .cfi_rel_offset ecx, 0
- mov 12(%esp), %ebx
- mov 16(%esp), %ecx
- movl $__NR_ftruncate, %eax
- int $0x80
- cmpl $-MAX_ERRNO, %eax
- jb 1f
- negl %eax
- pushl %eax
- call __set_errno_internal
- addl $4, %esp
-1:
- popl %ecx
- popl %ebx
- ret
-END(ftruncate)
diff --git a/libc/bionic/ftruncate.cpp b/libc/bionic/ftruncate.cpp
new file mode 100644
index 000000000..c073f2d43
--- /dev/null
+++ b/libc/bionic/ftruncate.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <sys/cdefs.h>
+#include <unistd.h>
+
+#if !defined(__USE_FILE_OFFSET64) && !defined(__LP64__)
+// The kernel's implementation of ftruncate uses an unsigned long for the length
+// parameter, so it will not catch negative values. On the other hand
+// ftruncate64 does check for this, so just forward the call.
+int ftruncate(int filedes, off_t length) {
+ return ftruncate64(filedes, length);
+}
+#endif
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index f54a4619b..a21578a89 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -176,6 +176,15 @@ TEST(unistd, ftruncate64) {
ASSERT_EQ(123, sb.st_size);
}
+TEST(unistd, ftruncate_negative) {
+ TemporaryFile tf;
+ errno = 0;
+ int rc = ftruncate(tf.fd, -123);
+ int err = errno;
+ ASSERT_EQ(-1, rc);
+ ASSERT_EQ(EINVAL, err);
+}
+
static bool g_pause_test_flag = false;
static void PauseTestSignalHandler(int) {
g_pause_test_flag = true;