Skip to content

Commit

Permalink
8183227: read/write APIs in class os shall return ssize_t
Browse files Browse the repository at this point in the history
Reviewed-by: fparain, rehn
  • Loading branch information
Harold Seigel committed Jan 10, 2022
1 parent 6613ce6 commit 4ff6720
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/hotspot/os/linux/attachListener_linux.cpp
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -385,7 +385,7 @@ LinuxAttachOperation* LinuxAttachListener::dequeue() {
// write the given buffer to the socket
int LinuxAttachListener::write_fully(int s, char* buf, int len) {
do {
int n = ::write(s, buf, len);
ssize_t n = ::write(s, buf, len);
if (n == -1) {
if (errno != EINTR) return -1;
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/os/posix/os_posix.cpp
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -679,9 +679,9 @@ FILE* os::open(int fd, const char* mode) {
return ::fdopen(fd, mode);
}

size_t os::write(int fd, const void *buf, unsigned int nBytes) {
size_t res;
RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
ssize_t os::write(int fd, const void *buf, unsigned int nBytes) {
ssize_t res;
RESTARTABLE(::write(fd, buf, (size_t) nBytes), res);
return res;
}

Expand Down
30 changes: 14 additions & 16 deletions src/hotspot/os/posix/perfMemory_posix.cpp
Original file line number Diff line number Diff line change
@@ -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.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -98,21 +98,20 @@ static void save_memory_to_file(char* addr, size_t size) {
const char* destfile = PerfMemory::get_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),
result);
if (result == OS_ERR) {
RESTARTABLE(os::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR), fd);
if (fd == OS_ERR) {
if (PrintMiscellaneous && Verbose) {
warning("Could not create Perfdata save file: %s: %s\n",
destfile, os::strerror(errno));
}
} else {
int fd = result;
ssize_t result;

for (size_t remaining = size; remaining > 0;) {

RESTARTABLE(::write(fd, addr, remaining), result);
result = os::write(fd, addr, remaining);
if (result == OS_ERR) {
if (PrintMiscellaneous && Verbose) {
warning("Could not write Perfdata save file: %s: %s\n",
Expand All @@ -125,7 +124,7 @@ static void save_memory_to_file(char* addr, size_t size) {
addr += result;
}

result = ::close(fd);
result = os::close(fd);
if (PrintMiscellaneous && Verbose) {
if (result == OS_ERR) {
warning("Could not close %s: %s\n", destfile, os::strerror(errno));
Expand Down Expand Up @@ -842,9 +841,9 @@ static int create_sharedmem_resources(const char* dirname, const char* filename,
// Open the filename in the current directory.
// Cannot use O_TRUNC here; truncation of an existing file has to happen
// after the is_file_secure() check below.
int result;
RESTARTABLE(os::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IRUSR|S_IWUSR), result);
if (result == OS_ERR) {
int fd;
RESTARTABLE(os::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IRUSR|S_IWUSR), fd);
if (fd == OS_ERR) {
if (PrintMiscellaneous && Verbose) {
if (errno == ELOOP) {
warning("file %s is a symlink and is not secure\n", filename);
Expand All @@ -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_directory_secure_cwd(dirp, saved_cwd_fd);

// save the file descriptor
int fd = result;

// check to see if the file is secure
if (!is_file_secure(fd, filename)) {
::close(fd);
return -1;
}

ssize_t result;

// truncate the file to get rid of any existing data
RESTARTABLE(::ftruncate(fd, (off_t)0), result);
if (result == OS_ERR) {
Expand All @@ -895,7 +893,7 @@ static int create_sharedmem_resources(const char* dirname, const char* filename,
int zero_int = 0;
result = (int)os::seek_to_file_offset(fd, (jlong)(seekpos));
if (result == -1 ) break;
RESTARTABLE(::write(fd, &zero_int, 1), result);
result = os::write(fd, &zero_int, 1);
if (result != 1) {
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);
Expand All @@ -907,7 +905,7 @@ static int create_sharedmem_resources(const char* dirname, const char* filename,
if (result != -1) {
return fd;
} else {
::close(fd);
os::close(fd);
return -1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -4758,7 +4758,7 @@ FILE* os::open(int fd, const char* 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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/cds/filemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1613,8 +1613,8 @@ size_t FileMapInfo::write_heap_regions(GrowableArray<MemRegion>* regions,

void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
assert(_file_open, "must be");
size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
if (n != nbytes) {
ssize_t n = os::write(_fd, buffer, (unsigned int)nbytes);
if (n < 0 || (size_t)n != nbytes) {
// If the shared archive is corrupted, close it and remove it.
close();
remove(_full_path);
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/jfr/writers/jfrStreamWriterHost.inline.hpp
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -76,7 +76,7 @@ inline void StreamWriterHost<Adapter, AP>::write_bytes(const u1* buf, intptr_t l
assert(len >= 0, "invariant");
while (len > 0) {
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");
_stream_pos += num_written;
len -= num_written;
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/prims/jvm.cpp
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -2836,7 +2836,7 @@ void jio_print(const char* s, size_t len) {
jio_fprintf(defaultStream::output_stream(), "%.*s", (int)len, s);
} else {
// 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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/runtime/os.hpp
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -565,7 +565,7 @@ class os: AllStatic {

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 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.
static DIR* opendir(const char* dirname);
Expand Down
3 changes: 2 additions & 1 deletion src/hotspot/share/services/heapDumperCompression.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -54,7 +55,7 @@ char const* FileWriter::write_buf(char* buf, ssize_t size) {
assert(_fd >= 0, "Must be open");
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) {
return os::strerror(errno);
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/utilities/ostream.cpp
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -614,7 +614,7 @@ void fileStream::flush() {
void fdStream::write(const char* s, size_t len) {
if (_fd != -1) {
// 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);
}
}
Expand Down

1 comment on commit 4ff6720

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.