8183227: read/write APIs in class os shall return ssize_t

Reviewed-by: fparain, rehn
This commit is contained in:
Harold Seigel 2022-01-10 13:18:41 +00:00
parent 6613ce64d7
commit 4ff6720573
10 changed files with 34 additions and 35 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -385,7 +385,7 @@ LinuxAttachOperation* LinuxAttachListener::dequeue() {
// write the given buffer to the socket // write the given buffer to the socket
int LinuxAttachListener::write_fully(int s, char* buf, int len) { int LinuxAttachListener::write_fully(int s, char* buf, int len) {
do { do {
int n = ::write(s, buf, len); ssize_t n = ::write(s, buf, len);
if (n == -1) { if (n == -1) {
if (errno != EINTR) return -1; if (errno != EINTR) return -1;
} else { } else {

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -679,9 +679,9 @@ FILE* os::open(int fd, const char* mode) {
return ::fdopen(fd, mode); return ::fdopen(fd, mode);
} }
size_t os::write(int fd, const void *buf, unsigned int nBytes) { ssize_t os::write(int fd, const void *buf, unsigned int nBytes) {
size_t res; ssize_t res;
RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res); RESTARTABLE(::write(fd, buf, (size_t) nBytes), res);
return res; return res;
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021 SAP SE. All rights reserved. * Copyright (c) 2012, 2021 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
@ -98,21 +98,20 @@ static void save_memory_to_file(char* addr, size_t size) {
const char* destfile = PerfMemory::get_perfdata_file_path(); const char* destfile = PerfMemory::get_perfdata_file_path();
assert(destfile[0] != '\0', "invalid PerfData file path"); assert(destfile[0] != '\0', "invalid PerfData file path");
int result; int fd;
RESTARTABLE(os::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR), RESTARTABLE(os::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR), fd);
result); if (fd == OS_ERR) {
if (result == OS_ERR) {
if (PrintMiscellaneous && Verbose) { if (PrintMiscellaneous && Verbose) {
warning("Could not create Perfdata save file: %s: %s\n", warning("Could not create Perfdata save file: %s: %s\n",
destfile, os::strerror(errno)); destfile, os::strerror(errno));
} }
} else { } else {
int fd = result; ssize_t result;
for (size_t remaining = size; remaining > 0;) { for (size_t remaining = size; remaining > 0;) {
RESTARTABLE(::write(fd, addr, remaining), result); result = os::write(fd, addr, remaining);
if (result == OS_ERR) { if (result == OS_ERR) {
if (PrintMiscellaneous && Verbose) { if (PrintMiscellaneous && Verbose) {
warning("Could not write Perfdata save file: %s: %s\n", warning("Could not write Perfdata save file: %s: %s\n",
@ -125,7 +124,7 @@ static void save_memory_to_file(char* addr, size_t size) {
addr += result; addr += result;
} }
result = ::close(fd); result = os::close(fd);
if (PrintMiscellaneous && Verbose) { if (PrintMiscellaneous && Verbose) {
if (result == OS_ERR) { if (result == OS_ERR) {
warning("Could not close %s: %s\n", destfile, os::strerror(errno)); warning("Could not close %s: %s\n", destfile, os::strerror(errno));
@ -842,9 +841,9 @@ static int create_sharedmem_resources(const char* dirname, const char* filename,
// Open the filename in the current directory. // Open the filename in the current directory.
// Cannot use O_TRUNC here; truncation of an existing file has to happen // Cannot use O_TRUNC here; truncation of an existing file has to happen
// after the is_file_secure() check below. // after the is_file_secure() check below.
int result; int fd;
RESTARTABLE(os::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IRUSR|S_IWUSR), result); RESTARTABLE(os::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IRUSR|S_IWUSR), fd);
if (result == OS_ERR) { if (fd == OS_ERR) {
if (PrintMiscellaneous && Verbose) { if (PrintMiscellaneous && Verbose) {
if (errno == ELOOP) { if (errno == ELOOP) {
warning("file %s is a symlink and is not secure\n", filename); warning("file %s is a symlink and is not secure\n", filename);
@ -860,15 +859,14 @@ static int create_sharedmem_resources(const char* dirname, const char* filename,
// close the directory and reset the current working directory // close the directory and reset the current working directory
close_directory_secure_cwd(dirp, saved_cwd_fd); close_directory_secure_cwd(dirp, saved_cwd_fd);
// save the file descriptor
int fd = result;
// check to see if the file is secure // check to see if the file is secure
if (!is_file_secure(fd, filename)) { if (!is_file_secure(fd, filename)) {
::close(fd); ::close(fd);
return -1; return -1;
} }
ssize_t result;
// truncate the file to get rid of any existing data // truncate the file to get rid of any existing data
RESTARTABLE(::ftruncate(fd, (off_t)0), result); RESTARTABLE(::ftruncate(fd, (off_t)0), result);
if (result == OS_ERR) { if (result == OS_ERR) {
@ -895,7 +893,7 @@ static int create_sharedmem_resources(const char* dirname, const char* filename,
int zero_int = 0; int zero_int = 0;
result = (int)os::seek_to_file_offset(fd, (jlong)(seekpos)); result = (int)os::seek_to_file_offset(fd, (jlong)(seekpos));
if (result == -1 ) break; if (result == -1 ) break;
RESTARTABLE(::write(fd, &zero_int, 1), result); result = os::write(fd, &zero_int, 1);
if (result != 1) { if (result != 1) {
if (errno == ENOSPC) { if (errno == ENOSPC) {
warning("Insufficient space for shared memory file:\n %s\nTry using the -Djava.io.tmpdir= option to select an alternate temp location.\n", filename); warning("Insufficient space for shared memory file:\n %s\nTry using the -Djava.io.tmpdir= option to select an alternate temp location.\n", filename);
@ -907,7 +905,7 @@ static int create_sharedmem_resources(const char* dirname, const char* filename,
if (result != -1) { if (result != -1) {
return fd; return fd;
} else { } else {
::close(fd); os::close(fd);
return -1; return -1;
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -4758,7 +4758,7 @@ FILE* os::open(int fd, const char* mode) {
return ::_fdopen(fd, mode); return ::_fdopen(fd, mode);
} }
size_t os::write(int fd, const void *buf, unsigned int nBytes) { ssize_t os::write(int fd, const void *buf, unsigned int nBytes) {
return ::write(fd, buf, nBytes); return ::write(fd, buf, nBytes);
} }

View file

@ -1613,8 +1613,8 @@ size_t FileMapInfo::write_heap_regions(GrowableArray<MemRegion>* regions,
void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) { void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
assert(_file_open, "must be"); assert(_file_open, "must be");
size_t n = os::write(_fd, buffer, (unsigned int)nbytes); ssize_t n = os::write(_fd, buffer, (unsigned int)nbytes);
if (n != nbytes) { if (n < 0 || (size_t)n != nbytes) {
// If the shared archive is corrupted, close it and remove it. // If the shared archive is corrupted, close it and remove it.
close(); close();
remove(_full_path); remove(_full_path);

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -76,7 +76,7 @@ inline void StreamWriterHost<Adapter, AP>::write_bytes(const u1* buf, intptr_t l
assert(len >= 0, "invariant"); assert(len >= 0, "invariant");
while (len > 0) { while (len > 0) {
const unsigned int nBytes = len > INT_MAX ? INT_MAX : (unsigned int)len; const unsigned int nBytes = len > INT_MAX ? INT_MAX : (unsigned int)len;
const ssize_t num_written = (ssize_t)os::write(_fd, buf, nBytes); const ssize_t num_written = os::write(_fd, buf, nBytes);
guarantee(num_written > 0, "Nothing got written, or os::write() failed"); guarantee(num_written > 0, "Nothing got written, or os::write() failed");
_stream_pos += num_written; _stream_pos += num_written;
len -= num_written; len -= num_written;

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -2836,7 +2836,7 @@ void jio_print(const char* s, size_t len) {
jio_fprintf(defaultStream::output_stream(), "%.*s", (int)len, s); jio_fprintf(defaultStream::output_stream(), "%.*s", (int)len, s);
} else { } else {
// Make an unused local variable to avoid warning from gcc compiler. // Make an unused local variable to avoid warning from gcc compiler.
size_t count = ::write(defaultStream::output_fd(), s, (int)len); ssize_t count = os::write(defaultStream::output_fd(), s, (int)len);
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -565,7 +565,7 @@ class os: AllStatic {
static ssize_t read(int fd, void *buf, unsigned int nBytes); static ssize_t read(int fd, void *buf, unsigned int nBytes);
static ssize_t read_at(int fd, void *buf, unsigned int nBytes, jlong offset); static ssize_t read_at(int fd, void *buf, unsigned int nBytes, jlong offset);
static size_t write(int fd, const void *buf, unsigned int nBytes); static ssize_t write(int fd, const void *buf, unsigned int nBytes);
// Reading directories. // Reading directories.
static DIR* opendir(const char* dirname); static DIR* opendir(const char* dirname);

View file

@ -1,4 +1,5 @@
/* /*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 SAP SE. All rights reserved. * Copyright (c) 2020 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
@ -54,7 +55,7 @@ char const* FileWriter::write_buf(char* buf, ssize_t size) {
assert(_fd >= 0, "Must be open"); assert(_fd >= 0, "Must be open");
assert(size > 0, "Must write at least one byte"); assert(size > 0, "Must write at least one byte");
ssize_t n = (ssize_t) os::write(_fd, buf, (uint) size); ssize_t n = os::write(_fd, buf, (uint) size);
if (n <= 0) { if (n <= 0) {
return os::strerror(errno); return os::strerror(errno);

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -614,7 +614,7 @@ void fileStream::flush() {
void fdStream::write(const char* s, size_t len) { void fdStream::write(const char* s, size_t len) {
if (_fd != -1) { if (_fd != -1) {
// Make an unused local variable to avoid warning from gcc compiler. // Make an unused local variable to avoid warning from gcc compiler.
size_t count = ::write(_fd, s, (int)len); ssize_t count = ::write(_fd, s, (int)len);
update_position(s, len); update_position(s, len);
} }
} }