Copilot commented on code in PR #7478: URL: https://github.com/apache/ignite-3/pull/7478#discussion_r2736195349
########## modules/platforms/cpp/tests/fake_server/socket_adapter/posix/server_socket_adapter.h: ########## @@ -0,0 +1,75 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <sys/socket.h> +#include <netinet/in.h> +#include <unistd.h> + +namespace ignite { +class server_socket_adapter { +public: + explicit server_socket_adapter(int m_fd) + : m_fd(m_fd) {} + + server_socket_adapter() = default; + + server_socket_adapter(const server_socket_adapter &other) = default; + + server_socket_adapter &operator=(const server_socket_adapter &other) = default; + Review Comment: `server_socket_adapter` is an owning wrapper around a socket handle but is copyable (default copy constructor and copy assignment) without any shared-ownership semantics, which makes double-closing or use-after-close of the underlying socket very easy by mistake. To make ownership clear and safer, consider deleting the copy constructor/assignment and adding move semantics, or otherwise ensuring only a single owner is responsible for closing the socket. ```suggestion server_socket_adapter(const server_socket_adapter &other) = delete; server_socket_adapter &operator=(const server_socket_adapter &other) = delete; server_socket_adapter(server_socket_adapter &&other) noexcept : m_fd(other.m_fd) { other.m_fd = -1; } server_socket_adapter &operator=(server_socket_adapter &&other) noexcept { if (this != &other) { if (is_valid()) { ::close(m_fd); } m_fd = other.m_fd; other.m_fd = -1; } return *this; } ``` ########## modules/platforms/cpp/tests/fake_server/socket_adapter/win/client_socket_adapter.h: ########## @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include <winsock2.h> +#include <vector> +#include <cstddef> + +namespace ignite { +class client_socket_adapter { +public: + explicit client_socket_adapter(SOCKET m_fd) + : m_fd(m_fd) {} + + client_socket_adapter() = default; + + client_socket_adapter(const client_socket_adapter &other) = default; + + client_socket_adapter &operator=(const client_socket_adapter &other) = default; + Review Comment: `client_socket_adapter` (Windows) is an owning wrapper for a `SOCKET` but is copyable with a `close()` method that unconditionally calls `closesocket` on the stored handle. This can lead to multiple copies managing the same `SOCKET` and potentially double-closing it; please consider deleting the copy constructor/assignment and using move-only semantics for clearer, safer ownership. ```suggestion client_socket_adapter(const client_socket_adapter &other) = delete; client_socket_adapter &operator=(const client_socket_adapter &other) = delete; client_socket_adapter(client_socket_adapter &&other) noexcept : m_fd(other.m_fd) { other.m_fd = INVALID_SOCKET; } client_socket_adapter &operator=(client_socket_adapter &&other) noexcept { if (this != &other) { m_fd = other.m_fd; other.m_fd = INVALID_SOCKET; } return *this; } ``` ########## modules/platforms/cpp/tests/fake_server/socket_adapter/win/server_socket_adapter.h: ########## @@ -0,0 +1,74 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <winsock2.h> +#include <ws2tcpip.h> + +namespace ignite { +class server_socket_adapter { +public: + explicit server_socket_adapter(SOCKET m_fd) + : m_fd(m_fd) {} + + server_socket_adapter() = default; + + server_socket_adapter(const server_socket_adapter &other) = default; + + server_socket_adapter &operator=(const server_socket_adapter &other) = default; + Review Comment: `server_socket_adapter` on Windows is an owning wrapper around `SOCKET` but remains copyable (default copy constructor and assignment) even though each instance calls `closesocket` in `close()`. This can easily lead to double-closing the same `SOCKET` or using a socket after it was closed via another copy; please make the type move-only or otherwise enforce single ownership of the handle. ```suggestion server_socket_adapter(const server_socket_adapter &other) = delete; server_socket_adapter &operator=(const server_socket_adapter &other) = delete; server_socket_adapter(server_socket_adapter &&other) noexcept : m_fd(other.m_fd) { other.m_fd = INVALID_SOCKET; } server_socket_adapter &operator=(server_socket_adapter &&other) noexcept { if (this != &other) { if (is_valid()) { ::closesocket(m_fd); } m_fd = other.m_fd; other.m_fd = INVALID_SOCKET; } return *this; } ``` ########## modules/platforms/cpp/tests/fake_server/socket_adapter/win/client_socket_adapter.h: ########## @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include <winsock2.h> +#include <vector> +#include <cstddef> + +namespace ignite { +class client_socket_adapter { +public: + explicit client_socket_adapter(SOCKET m_fd) + : m_fd(m_fd) {} + + client_socket_adapter() = default; + + client_socket_adapter(const client_socket_adapter &other) = default; + + client_socket_adapter &operator=(const client_socket_adapter &other) = default; + + bool is_valid() const { return m_fd != INVALID_SOCKET; } + + void send_message(const std::vector<std::byte> &msg) { + ::send(m_fd, reinterpret_cast<const char *>(msg.data()), msg.size(), 0); + } + + int receive_next_packet(std::byte *buf, size_t buf_size) { + return ::recv(m_fd, reinterpret_cast<char *>(buf), buf_size, 0); Review Comment: On Windows, `receive_next_packet` takes a `size_t buf_size` but passes it directly to `::recv`, whose length parameter is an `int`. This implicit narrowing can overflow for large buffers and is harder to reason about; it would be safer to validate that `buf_size` fits into an `int` and cast explicitly, or to use an `int` parameter type that matches the underlying API. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
