Fix error handling for negative size in ftruncate.

Bug: 21309901
Change-Id: I54692ab8105dd09db6af7a2c0894a17bdd118aa0
(cherry picked from commit c05554ec5c9aff5e0f1e83de9bb62c3569eecca2)
This commit is contained in:
Dan Albert 2015-05-19 18:17:31 -07:00
parent ca10ac6dd4
commit 5f3e19dbbe
7 changed files with 38 additions and 60 deletions

View File

@ -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 \

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)

28
libc/bionic/ftruncate.cpp Normal file
View File

@ -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

View File

@ -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;