Fix error handling for negative size in ftruncate.

Bug: 21309901
Change-Id: I54692ab8105dd09db6af7a2c0894a17bdd118aa0
(cherry picked from commit c05554ec5c9aff5e0f1e83de9bb62c3569eecca2)
diff --git a/libc/Android.mk b/libc/Android.mk
index f09733e..d6274dd 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -131,6 +131,7 @@
     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 0246833..9d7f839 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -151,7 +151,6 @@
 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 1bfe2f3..0000000
--- 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 0589c81..0000000
--- 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 78d1e18..0000000
--- 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 0000000..c073f2d
--- /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 f54a461..a21578a 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -176,6 +176,15 @@
   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;