summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/StaticAssert.h28
-rw-r--r--luni/src/main/native/NativeBN.cpp9
-rw-r--r--luni/src/main/native/java_io_File.cpp4
-rw-r--r--luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp41
-rw-r--r--luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp1
5 files changed, 51 insertions, 32 deletions
diff --git a/include/StaticAssert.h b/include/StaticAssert.h
new file mode 100644
index 0000000..ab809b3
--- /dev/null
+++ b/include/StaticAssert.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+#ifndef STATIC_ASSERT_H_included
+#define STATIC_ASSERT_H_included
+
+/**
+ * Similar to C++0x's static_assert. Message argument must be a valid identifier, not a string.
+ * Called COMPILE_ASSERT in Google, COMPILE_TIME_ASSERT in other places. This is the usual Google
+ * implementation.
+ */
+#define STATIC_ASSERT(exp, msg) typedef StaticAssert<(bool(exp))> msg[bool(exp) ? 1 : -1]
+template <bool> struct StaticAssert {};
+
+#endif // STATIC_ASSERT_H_included
diff --git a/luni/src/main/native/NativeBN.cpp b/luni/src/main/native/NativeBN.cpp
index 4ef8c28..6359e3e 100644
--- a/luni/src/main/native/NativeBN.cpp
+++ b/luni/src/main/native/NativeBN.cpp
@@ -24,9 +24,9 @@
#include "JniConstants.h"
#include "ScopedPrimitiveArray.h"
#include "ScopedUtfChars.h"
+#include "StaticAssert.h"
#include "UniquePtr.h"
#include "jni.h"
-#include <assert.h>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
@@ -169,7 +169,7 @@ static jboolean NativeBN_litEndInts2bn(JNIEnv* env, jclass, jintArray arr, int l
return JNI_FALSE;
}
- assert(sizeof(BN_ULONG) == sizeof(jint));
+ STATIC_ASSERT(sizeof(BN_ULONG) == sizeof(jint), BN_ULONG_not_32_bit);
const BN_ULONG* tmpInts = reinterpret_cast<const BN_ULONG*>(scopedArray.get());
if ((tmpInts != NULL) && (bn_wexpand(ret, len) != NULL)) {
int i = len; do { i--; ret->d[i] = tmpInts[i]; } while (i > 0);
@@ -197,10 +197,9 @@ static jboolean NativeBN_litEndInts2bn(JNIEnv* env, jclass, jintArray arr, int l
| (bytes[k + 0] & 0xFF) << 24 )
static jboolean negBigEndianBytes2bn(JNIEnv*, jclass, const unsigned char* bytes, int bytesLen, BIGNUM* ret) {
-// We rely on: (BN_BITS2 == 32), i.e. BN_ULONG is unsigned int and has 4 bytes:
-//
+ // We rely on: (BN_BITS2 == 32), i.e. BN_ULONG is unsigned int and has 4 bytes:
bn_check_top(ret);
-// FIXME: ASSERT (bytesLen > 0);
+ // FIXME: assert bytesLen > 0
int intLen = (bytesLen + 3) / 4;
int firstNonzeroDigit = -2;
if (bn_wexpand(ret, intLen) != NULL) {
diff --git a/luni/src/main/native/java_io_File.cpp b/luni/src/main/native/java_io_File.cpp
index e14cf73..ea8d12b 100644
--- a/luni/src/main/native/java_io_File.cpp
+++ b/luni/src/main/native/java_io_File.cpp
@@ -24,6 +24,7 @@
#include "ScopedLocalRef.h"
#include "ScopedPrimitiveArray.h"
#include "ScopedUtfChars.h"
+#include "StaticAssert.h"
#include <dirent.h>
#include <errno.h>
@@ -208,6 +209,7 @@ static jlong File_getFreeSpaceImpl(JNIEnv* env, jclass, jstring javaPath) {
if (!doStatFs(env, javaPath, sb)) {
return 0;
}
+ STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit);
return sb.f_bfree * sb.f_bsize; // free block count * block size in bytes.
}
@@ -216,6 +218,7 @@ static jlong File_getTotalSpaceImpl(JNIEnv* env, jclass, jstring javaPath) {
if (!doStatFs(env, javaPath, sb)) {
return 0;
}
+ STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit);
return sb.f_blocks * sb.f_bsize; // total block count * block size in bytes.
}
@@ -224,6 +227,7 @@ static jlong File_getUsableSpaceImpl(JNIEnv* env, jclass, jstring javaPath) {
if (!doStatFs(env, javaPath, sb)) {
return 0;
}
+ STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit);
return sb.f_bavail * sb.f_bsize; // non-root free block count * block size in bytes.
}
diff --git a/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp b/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp
index db5a7ee..346eef5 100644
--- a/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp
+++ b/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp
@@ -37,7 +37,6 @@
#include "ScopedUtfChars.h"
#include "UniquePtr.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@@ -106,7 +105,7 @@ static int EsTranslateOpenFlags(int flags) {
// Checks whether we can safely treat the given jlong as an off_t without
// accidental loss of precision.
-// TODO: this is bogus; we should use _FILE_OFFSET_BITS=64.
+// TODO: this is bogus; we should use _FILE_OFFSET_BITS=64, but bionic doesn't support it.
static bool offsetTooLarge(JNIEnv* env, jlong longOffset) {
if (sizeof(off_t) >= sizeof(jlong)) {
// We're only concerned about the possibility that off_t is
@@ -116,7 +115,11 @@ static bool offsetTooLarge(JNIEnv* env, jlong longOffset) {
}
// TODO: use std::numeric_limits<off_t>::max() and min() when we have them.
- assert(sizeof(off_t) == sizeof(int));
+ if (sizeof(off_t) != sizeof(int)) {
+ jniThrowException(env, "java/lang/AssertionError",
+ "off_t is smaller than 64-bit, but not 32-bit!");
+ return true;
+ }
static const off_t off_t_max = INT_MAX;
static const off_t off_t_min = INT_MIN;
@@ -136,8 +139,8 @@ static jlong translateLockLength(jlong length) {
return (length == 0x7fffffffffffffffLL) ? 0 : length;
}
-static struct flock flockFromStartAndLength(jlong start, jlong length) {
- struct flock lock;
+static struct flock64 flockFromStartAndLength(jlong start, jlong length) {
+ struct flock64 lock;
memset(&lock, 0, sizeof(lock));
lock.l_whence = SEEK_SET;
@@ -147,15 +150,11 @@ static struct flock flockFromStartAndLength(jlong start, jlong length) {
return lock;
}
-static jint OSFileSystem_lockImpl(JNIEnv* env, jobject, jint fd,
+static jint OSFileSystem_lockImpl(JNIEnv*, jobject, jint fd,
jlong start, jlong length, jint typeFlag, jboolean waitFlag) {
length = translateLockLength(length);
- if (offsetTooLarge(env, start) || offsetTooLarge(env, length)) {
- return -1;
- }
-
- struct flock lock(flockFromStartAndLength(start, length));
+ struct flock64 lock(flockFromStartAndLength(start, length));
if ((typeFlag & SHARED_LOCK_TYPE) == SHARED_LOCK_TYPE) {
lock.l_type = F_RDLCK;
@@ -163,20 +162,16 @@ static jint OSFileSystem_lockImpl(JNIEnv* env, jobject, jint fd,
lock.l_type = F_WRLCK;
}
- int waitMode = (waitFlag) ? F_SETLKW : F_SETLK;
+ int waitMode = (waitFlag) ? F_SETLKW64 : F_SETLK64;
return TEMP_FAILURE_RETRY(fcntl(fd, waitMode, &lock));
}
static void OSFileSystem_unlockImpl(JNIEnv* env, jobject, jint fd, jlong start, jlong length) {
length = translateLockLength(length);
- if (offsetTooLarge(env, start) || offsetTooLarge(env, length)) {
- return;
- }
-
- struct flock lock(flockFromStartAndLength(start, length));
+ struct flock64 lock(flockFromStartAndLength(start, length));
lock.l_type = F_UNLCK;
- int rc = TEMP_FAILURE_RETRY(fcntl(fd, F_SETLKW, &lock));
+ int rc = TEMP_FAILURE_RETRY(fcntl(fd, F_SETLKW64, &lock));
if (rc == -1) {
jniThrowIOException(env, errno);
}
@@ -337,14 +332,7 @@ static jlong OSFileSystem_seek(JNIEnv* env, jobject, jint fd, jlong offset, jint
return -1;
}
- // If the offset is relative, lseek(2) will tell us whether it's too large.
- // We're just worried about too large an absolute offset, which would cause
- // us to lie to lseek(2).
- if (offsetTooLarge(env, offset)) {
- return -1;
- }
-
- jlong result = lseek(fd, offset, nativeWhence);
+ jlong result = lseek64(fd, offset, nativeWhence);
if (result == -1) {
jniThrowIOException(env, errno);
}
@@ -359,6 +347,7 @@ static void OSFileSystem_fsync(JNIEnv* env, jobject, jint fd, jboolean metadataT
}
static jint OSFileSystem_truncate(JNIEnv* env, jobject, jint fd, jlong length) {
+ // TODO: if we had ftruncate64, we could kill this (http://b/3107933).
if (offsetTooLarge(env, length)) {
return -1;
}
diff --git a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
index 188839d..7d3a0cf 100644
--- a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
+++ b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
@@ -28,7 +28,6 @@
#include "valueOf.h"
#include <arpa/inet.h>
-#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>