8289778: ZGC: incorrect use of os::free() for mountpoint string handling after JDK-8289633

Reviewed-by: stuefe, dholmes, mdoerr
This commit is contained in:
Jie Fu 2022-07-07 12:52:04 +00:00
parent 532a6ec7e3
commit 77ad998b6e

View File

@ -28,6 +28,7 @@
#include "gc/z/zMountPoint_linux.hpp"
#include "runtime/globals.hpp"
#include "runtime/os.hpp"
#include "utilities/globalDefinitions.hpp"
#include <stdio.h>
#include <unistd.h>
@ -57,15 +58,16 @@ char* ZMountPoint::get_mountpoint(const char* line, const char* filesystem) cons
// Parse line and return a newly allocated string containing the mount point if
// the line contains a matching filesystem and the mount point is accessible by
// the current user.
// sscanf, using %m, will return malloced memory. Need raw ::free, not os::free.
if (sscanf(line, "%*u %*u %*u:%*u %*s %ms %*[^-]- %ms", &line_mountpoint, &line_filesystem) != 2 ||
strcmp(line_filesystem, filesystem) != 0 ||
access(line_mountpoint, R_OK|W_OK|X_OK) != 0) {
// Not a matching or accessible filesystem
os::free(line_mountpoint);
ALLOW_C_FUNCTION(::free, ::free(line_mountpoint);)
line_mountpoint = NULL;
}
os::free(line_filesystem);
ALLOW_C_FUNCTION(::free, ::free(line_filesystem);)
return line_mountpoint;
}
@ -88,14 +90,15 @@ void ZMountPoint::get_mountpoints(const char* filesystem, ZArray<char*>* mountpo
}
}
os::free(line);
// readline will return malloced memory. Need raw ::free, not os::free.
ALLOW_C_FUNCTION(::free, ::free(line);)
fclose(fd);
}
void ZMountPoint::free_mountpoints(ZArray<char*>* mountpoints) const {
ZArrayIterator<char*> iter(mountpoints);
for (char* mountpoint; iter.next(&mountpoint);) {
os::free(mountpoint);
ALLOW_C_FUNCTION(::free, ::free(mountpoint);) // *not* os::free
}
mountpoints->clear();
}