8202794: Native Unix code should use readdir rather than readdir_r
Reviewed-by: alanb, bsrbnd
Contributed-by: Bernard Blaser <bsrbnd@gmail.com>
--- a/src/java.base/unix/native/libjava/TimeZone_md.c Tue Jul 17 11:58:53 2018 -0700
+++ b/src/java.base/unix/native/libjava/TimeZone_md.c Tue Jul 17 12:03:10 2018 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -52,7 +52,7 @@
#if defined(_ALLBSD_SOURCE)
#define dirent64 dirent
-#define readdir64_r readdir_r
+#define readdir64 readdir
#endif
#if !defined(__solaris__) || defined(__sparcv9) || defined(amd64)
@@ -122,32 +122,18 @@
DIR *dirp = NULL;
struct stat statbuf;
struct dirent64 *dp = NULL;
- struct dirent64 *entry = NULL;
char *pathname = NULL;
int fd = -1;
char *dbuf = NULL;
char *tz = NULL;
int res;
- long name_max = 0;
dirp = opendir(dir);
if (dirp == NULL) {
return NULL;
}
- name_max = pathconf(dir, _PC_NAME_MAX);
- // If pathconf did not work, fall back to a mimimum buffer size.
- if (name_max < 1024) {
- name_max = 1024;
- }
-
- entry = (struct dirent64 *)malloc(offsetof(struct dirent64, d_name) + name_max + 1);
- if (entry == NULL) {
- (void) closedir(dirp);
- return NULL;
- }
-
- while (readdir64_r(dirp, entry, &dp) == 0 && dp != NULL) {
+ while ((dp = readdir64(dirp)) != NULL) {
/*
* Skip '.' and '..' (and possibly other .* files)
*/
@@ -214,9 +200,6 @@
pathname = NULL;
}
- if (entry != NULL) {
- free((void *) entry);
- }
if (dirp != NULL) {
(void) closedir(dirp);
}
--- a/src/java.base/unix/native/libjava/UnixFileSystem_md.c Tue Jul 17 11:58:53 2018 -0700
+++ b/src/java.base/unix/native/libjava/UnixFileSystem_md.c Tue Jul 17 12:03:10 2018 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -65,7 +65,7 @@
#if defined(_ALLBSD_SOURCE)
#define dirent64 dirent
- #define readdir64_r readdir_r
+ #define readdir64 readdir
#define stat64 stat
#ifndef MACOSX
#define statvfs64 statvfs
@@ -312,7 +312,6 @@
{
DIR *dir = NULL;
struct dirent64 *ptr;
- struct dirent64 *result;
int len, maxlen;
jobjectArray rv, old;
jclass str_class;
@@ -325,13 +324,6 @@
} END_PLATFORM_STRING(env, path);
if (dir == NULL) return NULL;
- ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1));
- if (ptr == NULL) {
- JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
- closedir(dir);
- return NULL;
- }
-
/* Allocate an initial String array */
len = 0;
maxlen = 16;
@@ -339,7 +331,7 @@
if (rv == NULL) goto error;
/* Scan the directory */
- while ((readdir64_r(dir, ptr, &result) == 0) && (result != NULL)) {
+ while ((ptr = readdir64(dir)) != NULL) {
jstring name;
if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
continue;
@@ -360,7 +352,6 @@
(*env)->DeleteLocalRef(env, name);
}
closedir(dir);
- free(ptr);
/* Copy the final results into an appropriately-sized array */
old = rv;
@@ -375,7 +366,6 @@
error:
closedir(dir);
- free(ptr);
return NULL;
}
--- a/src/jdk.management/unix/native/libmanagement_ext/OperatingSystemImpl.c Tue Jul 17 11:58:53 2018 -0700
+++ b/src/jdk.management/unix/native/libmanagement_ext/OperatingSystemImpl.c Tue Jul 17 12:03:10 2018 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -74,19 +74,9 @@
#endif /* _ALLBSD_SOURCE */
-static struct dirent* read_dir(DIR* dirp, struct dirent* entry) {
-#ifdef __solaris__
- struct dirent* dbuf = readdir(dirp);
- return dbuf;
-#else /* __linux__ || _ALLBSD_SOURCE */
- struct dirent* p;
- if (readdir_r(dirp, entry, &p) == 0) {
- return p;
- } else {
- return NULL;
- }
+#if defined(_ALLBSD_SOURCE)
+ #define readdir64 readdir
#endif
-}
// true = get available swap in bytes
// false = get total swap in bytes
@@ -432,7 +422,6 @@
return (100);
#else /* solaris/linux */
DIR *dirp;
- struct dirent dbuf;
struct dirent* dentp;
jlong fds = 0;
@@ -453,7 +442,7 @@
// iterate through directory entries, skipping '.' and '..'
// each entry represents an open file descriptor.
- while ((dentp = read_dir(dirp, &dbuf)) != NULL) {
+ while ((dentp = readdir64(dirp)) != NULL) {
if (isdigit(dentp->d_name[0])) {
fds++;
}