Skip to content

Commit

Permalink
fix: Switch to std:: functions in cstdlib and cstdio
Browse files Browse the repository at this point in the history
Bug: N/A
Change-Id: I164a96d75fd2d2dd6b7a5e94f931fc6e9ed63d42
GitOrigin-RevId: 2e7eae6ac6b3fbba6b77287223e2f016a1dce71a
  • Loading branch information
Privacy Sandbox Team authored and copybara-github committed Sep 30, 2024
1 parent 142b145 commit df80529
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 75 deletions.
34 changes: 18 additions & 16 deletions src/aws/proxy/client_session_pool_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <gtest/gtest.h>

#include <cstdlib>
#include <memory>
#include <thread>
#include <utility>
Expand Down Expand Up @@ -80,47 +81,48 @@ TEST(ClientSessionPoolTest, TestBind) {
socket_vendor::BindResponse bind_resp;
asio::read(client_sock0, mutable_buffer(&bind_resp, sizeof(bind_resp)), ec);
if (ec.failed()) {
exit(1);
std::exit(1);
}
socket_vendor::ListenResponse listen_resp;
asio::read(client_sock0, mutable_buffer(&listen_resp, sizeof(listen_resp)),
ec);
if (ec.failed()) {
exit(1);
std::exit(1);
}
// Try to receive a FD
uint8_t client_buff[128];
struct msghdr msg = {};
struct iovec iov = {client_buff, sizeof(client_buff)};
msg.msg_iov = &iov;
msg.msg_iovlen = 1;

union {
struct cmsghdr align;
char buf[CMSG_SPACE(sizeof(int))];
} cmsgu;

msg.msg_control = cmsgu.buf;
msg.msg_controllen = sizeof(cmsgu.buf);
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsgu.buf,
.msg_controllen = sizeof(cmsgu.buf),
};
// client_sock0.wait(Socket::wait_read);
ssize_t bytes_recv = recvmsg(client_sock0.native_handle(), &msg, 0);
if (bytes_recv <= 0) {
exit(2);
if (ssize_t bytes_recv = recvmsg(client_sock0.native_handle(), &msg, 0);
bytes_recv <= 0) {
std::exit(2);
} else {
LOG(INFO) << "Client received " << bytes_recv << " bytes";
}
LOG(INFO) << "Client received " << bytes_recv << " bytes";
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg == nullptr) {
exit(3);
std::exit(3);
}
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
exit(4);
std::exit(4);
}
int fd = -1;
memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
if (fd <= 0) {
exit(5);
std::exit(5);
}
exit(0);
std::exit(0);
}

asio::local::stream_protocol::socket proxy_client_sock(io_context);
Expand Down
36 changes: 19 additions & 17 deletions src/core/blob_storage_provider/gcp/gcp_cloud_storage_stress_run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ int ClearBucketIfPresent(Client& client) {
<< std::endl;
return EXIT_FAILURE;
}
auto status = client.DeleteObject(kBucketName, obj_metadata->name());
if (!status.ok()) {
if (auto status = client.DeleteObject(kBucketName, obj_metadata->name());
!status.ok()) {
std::cout << status.message() << std::endl;
return EXIT_FAILURE;
}
Expand Down Expand Up @@ -97,14 +97,14 @@ void WriteObjectOfByteCount(GcpCloudStorageClient& client, int64_t byte_count) {
put_blob_context.request->buffer->capacity;
put_blob_context.request->buffer->bytes->assign(byte_count, kBlobByte);

assert(client.PutBlob(put_blob_context).Successful());
::assert(client.PutBlob(put_blob_context).Successful());

while (!finished) {
}

if (!result.Successful()) {
std::cerr << errors::GetErrorMessage(result.status_code) << std::endl;
exit(EXIT_FAILURE);
std::exit(EXIT_FAILURE);
}
}

Expand All @@ -119,9 +119,9 @@ int WriteAndGetBlob(int64_t byte_count) {
CreateBucketIfNotExists(*client);

auto async_executor =
std::make_shared<AsyncExecutor>(kThreadCount, kQueueSize),
io_async_executor =
std::make_shared<AsyncExecutor>(kThreadCount, kQueueSize);
std::make_shared<AsyncExecutor>(kThreadCount, kQueueSize);
auto io_async_executor =
std::make_shared<AsyncExecutor>(kThreadCount, kQueueSize);

async_executor->Init();
async_executor->Run();
Expand All @@ -134,9 +134,10 @@ int WriteAndGetBlob(int64_t byte_count) {
WriteObjectOfByteCount(my_client, byte_count);

AsyncContext<GetBlobRequest, GetBlobResponse> get_blob_context;
get_blob_context.request = std::make_shared<GetBlobRequest>(
GetBlobRequest{std::make_shared<std::string>(kBucketName),
std::make_shared<std::string>(kDefaultBlobName)});
get_blob_context.request = std::make_shared<GetBlobRequest>(GetBlobRequest{
std::make_shared<std::string>(kBucketName),
std::make_shared<std::string>(kDefaultBlobName),
});

int return_status;
std::atomic_bool finished(false);
Expand Down Expand Up @@ -194,16 +195,17 @@ int WriteAndGetBlob(int64_t byte_count) {
} // namespace
} // namespace google::scp::core::test

constexpr int64_t kBytesCount = 100, kKiloBytesCount = 1000,
kMegaBytesCount = 1000 * kKiloBytesCount,
kGigaBytesCount = 1000 * kMegaBytesCount,
k10GigaBytesCount = 10 * kGigaBytesCount,
kTeraBytesCount = 1000 * kGigaBytesCount;
constexpr int64_t kBytesCount = 100;
constexpr int64_t kKiloBytesCount = 1000;
constexpr int64_t kMegaBytesCount = 1000 * kKiloBytesCount;
constexpr int64_t kGigaBytesCount = 1000 * kMegaBytesCount;
constexpr int64_t k10GigaBytesCount = 10 * kGigaBytesCount;
constexpr int64_t kTeraBytesCount = 1000 * kGigaBytesCount;

int main(int argc, char* argv[]) {
for (auto count :
{kBytesCount, kKiloBytesCount, kMegaBytesCount,
kGigaBytesCount /*, k10GigaBytesCount, kTeraBytesCount*/}) {
{kBytesCount, kKiloBytesCount, kMegaBytesCount, kGigaBytesCount,
/*, k10GigaBytesCount, kTeraBytesCount*/}) {
if (google::scp::core::test::WriteAndGetBlob(count) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
Expand Down
29 changes: 16 additions & 13 deletions src/core/test/utils/http1_helper/test_http1_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
#include "test_http1_server.h"

#include <cstdlib>
#include <string>
#include <utility>

Expand All @@ -31,33 +32,35 @@ void HandleErrorIfPresent(const boost::system::error_code& ec,
std::string stage) {
if (ec) {
std::cerr << stage << " failed: " << ec << std::endl;
exit(EXIT_FAILURE);
std::exit(EXIT_FAILURE);
}
}
} // namespace

// Uses the C socket library to bind to an unused port, close that socket then
// return that port number.
ExecutionResultOr<in_port_t> GetUnusedPortNumber() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
ExecutionResultOr<::in_port_t> GetUnusedPortNumber() {
int sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
return FailureExecutionResult(
errors::SC_TEST_HTTP1_SERVER_ERROR_GETTING_SOCKET);
}
sockaddr_in server_addr;
socklen_t server_len = sizeof(server_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = 0;
::sockaddr_in server_addr = {
.sin_family = AF_INET,
.sin_port = 0,
};
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, reinterpret_cast<sockaddr*>(&server_addr), server_len) < 0) {
::socklen_t server_len = sizeof(server_addr);
if (::bind(sockfd, reinterpret_cast<::sockaddr*>(&server_addr), server_len) <
0) {
return FailureExecutionResult(errors::SC_TEST_HTTP1_SERVER_ERROR_BINDING);
}
if (getsockname(sockfd, reinterpret_cast<sockaddr*>(&server_addr),
&server_len) < 0) {
if (::getsockname(sockfd, reinterpret_cast<::sockaddr*>(&server_addr),
&server_len) < 0) {
return FailureExecutionResult(
errors::SC_TEST_HTTP1_SERVER_ERROR_GETTING_SOCKET_NAME);
}
close(sockfd);
::close(sockfd);
return server_addr.sin_port;
}

Expand All @@ -79,7 +82,7 @@ TestHttp1Server::TestHttp1Server() {
ReadFromSocketAndWriteResponse(socket);
} else {
std::cerr << "accept failed: " << ec << std::endl;
exit(EXIT_FAILURE);
std::exit(EXIT_FAILURE);
}
});
{
Expand Down Expand Up @@ -128,7 +131,7 @@ void TestHttp1Server::ReadFromSocketAndWriteResponse(tcp::socket& socket) {
HandleErrorIfPresent(ec, "close");
}

in_port_t TestHttp1Server::PortNumber() const { return port_number_; }
::in_port_t TestHttp1Server::PortNumber() const { return port_number_; }

std::string TestHttp1Server::GetPath() const {
return "http://localhost:" + std::to_string(port_number_);
Expand Down
6 changes: 3 additions & 3 deletions src/core/test/utils/http1_helper/test_http1_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
namespace google::scp::core::test {

// Returns an unused TCP port number.
ExecutionResultOr<in_port_t> GetUnusedPortNumber();
ExecutionResultOr<::in_port_t> GetUnusedPortNumber();

// Lightweight Boost HTTP/1.1 server.
// After the constructor returns, the server is ready to accept incoming
Expand All @@ -46,7 +46,7 @@ class TestHttp1Server {
TestHttp1Server();

// Gets the port number the server is running on.
in_port_t PortNumber() const;
::in_port_t PortNumber() const;

// Gets the full path to this server i.e. 'http://localhost:8080'
std::string GetPath() const;
Expand Down Expand Up @@ -92,7 +92,7 @@ class TestHttp1Server {
absl::Mutex has_run_mu_;
bool has_run_ ABSL_GUARDED_BY(has_run_mu_) = false;

in_port_t port_number_ = 0;
::in_port_t port_number_ = 0;

// Indicates when thread should exit (false).
std::atomic_bool run_{true};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <sys/wait.h>

#include <cstdio>
#include <cstdlib>
#include <string>
#include <string_view>

Expand All @@ -29,25 +31,25 @@ namespace google::scp::cpio::client_providers::utils {

absl::StatusOr<std::string> Exec(absl::Span<const char* const> args) noexcept {
int fd[2];
if (pipe(fd) == -1) {
if (::pipe(fd) == -1) {
return absl::ErrnoToStatus(errno, "Exec failed to create a pipe.");
}
const pid_t pid = fork();
const ::pid_t pid = fork();
if (pid == 0) {
close(fd[0]);
::close(fd[0]);

// Redirect child standard output to pipe and execute.
if (dup2(fd[1], STDOUT_FILENO) == -1 ||
execv(args[0], const_cast<char* const*>(&args[0])) == -1) {
exit(errno);
if (::dup2(fd[1], STDOUT_FILENO) == -1 ||
::execv(args[0], const_cast<char* const*>(&args[0])) == -1) {
std::exit(errno);
}
} else if (pid == -1) {
return absl::ErrnoToStatus(errno, "Exec failed to fork a child.");
}

// Only parent gets here (pid > 0).
close(fd[1]);
if (int status; waitpid(pid, &status, /*options=*/0) == -1) {
::close(fd[1]);
if (int status; ::waitpid(pid, &status, /*options=*/0) == -1) {
return absl::ErrnoToStatus(errno, "Exec failed to wait for child.");
} else if (!WIFEXITED(status)) {
return absl::InternalError(absl::StrCat(
Expand All @@ -62,9 +64,9 @@ absl::StatusOr<std::string> Exec(absl::Span<const char* const> args) noexcept {
absl::StrJoin(args, " "), "'"));
}
std::string result;
if (FILE* const stream = fdopen(fd[0], "r"); stream != nullptr) {
if (FILE* const stream = ::fdopen(fd[0], "r"); stream != nullptr) {
std::array<char, 1024> buffer;
while (fgets(buffer.data(), buffer.size(), stream) != nullptr) {
while (std::fgets(buffer.data(), buffer.size(), stream) != nullptr) {
result += buffer.data();
}
} else {
Expand Down
Loading

0 comments on commit df80529

Please sign in to comment.