1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:17:44 +00:00

LibC: Prefer strlcpy over strcpy/strncpy

All of these are cosmetic (I believe). Furthermore, they serve as
reminders to always check the length of the destination buffers.
This commit is contained in:
Ben Wiederhake 2020-08-23 19:08:02 +02:00 committed by Andreas Kling
parent aa36e9917c
commit d419a780ae
3 changed files with 13 additions and 13 deletions

View file

@ -86,7 +86,7 @@ static int connect_to_lookup_server()
sockaddr_un address; sockaddr_un address;
address.sun_family = AF_LOCAL; address.sun_family = AF_LOCAL;
strcpy(address.sun_path, "/tmp/portal/lookup"); strlcpy(address.sun_path, "/tmp/portal/lookup", sizeof(address.sun_path));
if (connect(fd, (const sockaddr*)&address, sizeof(address)) < 0) { if (connect(fd, (const sockaddr*)&address, sizeof(address)) < 0) {
perror("connect_to_lookup_server"); perror("connect_to_lookup_server");
@ -102,7 +102,7 @@ hostent* gethostbyname(const char* name)
if (ipv4_address.has_value()) { if (ipv4_address.has_value()) {
auto ip4_string = ipv4_address.value().to_string(); auto ip4_string = ipv4_address.value().to_string();
ASSERT(ip4_string.length() < sizeof(__gethostbyname_name_buffer)); ASSERT(ip4_string.length() < sizeof(__gethostbyname_name_buffer));
strncpy(__gethostbyname_name_buffer, ip4_string.characters(), ip4_string.length()); strlcpy(__gethostbyname_name_buffer, ip4_string.characters(), sizeof(__gethostbyname_name_buffer));
__gethostbyname_buffer.h_name = __gethostbyname_name_buffer; __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
__gethostbyname_buffer.h_aliases = nullptr; __gethostbyname_buffer.h_aliases = nullptr;
__gethostbyname_buffer.h_addrtype = AF_INET; __gethostbyname_buffer.h_addrtype = AF_INET;
@ -152,7 +152,7 @@ hostent* gethostbyname(const char* name)
if (rc <= 0) if (rc <= 0)
return nullptr; return nullptr;
strncpy(__gethostbyname_name_buffer, name, sizeof(__gethostbyaddr_name_buffer) - 1); strlcpy(__gethostbyname_name_buffer, name, sizeof(__gethostbyaddr_name_buffer));
__gethostbyname_buffer.h_name = __gethostbyname_name_buffer; __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
__gethostbyname_buffer.h_aliases = nullptr; __gethostbyname_buffer.h_aliases = nullptr;
@ -216,7 +216,7 @@ hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
auto& response = responses[0]; auto& response = responses[0];
strncpy(__gethostbyaddr_name_buffer, response.characters(), max(sizeof(__gethostbyaddr_name_buffer), response.length())); strlcpy(__gethostbyaddr_name_buffer, response.characters(), sizeof(__gethostbyaddr_name_buffer));
__gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer; __gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer;
__gethostbyaddr_buffer.h_aliases = nullptr; __gethostbyaddr_buffer.h_aliases = nullptr;
@ -374,7 +374,7 @@ static bool fill_getserv_buffers(char* line, ssize_t read)
return false; return false;
} }
if (sizeof(__getserv_name_buffer) >= split_line[0].length() + 1) { if (sizeof(__getserv_name_buffer) >= split_line[0].length() + 1) {
strncpy(__getserv_name_buffer, split_line[0].characters(), split_line[0].length() + 1); strlcpy(__getserv_name_buffer, split_line[0].characters(), sizeof(__getserv_name_buffer));
} else { } else {
perror("invalid buffer length: service name"); perror("invalid buffer length: service name");
return false; return false;
@ -397,7 +397,7 @@ static bool fill_getserv_buffers(char* line, ssize_t read)
port_protocol_split[1].replace("\n", "", true); port_protocol_split[1].replace("\n", "", true);
if (sizeof(__getserv_protocol_buffer) >= port_protocol_split[1].length()) { if (sizeof(__getserv_protocol_buffer) >= port_protocol_split[1].length()) {
strncpy(__getserv_protocol_buffer, port_protocol_split[1].characters(), port_protocol_split[1].length() + 1); strlcpy(__getserv_protocol_buffer, port_protocol_split[1].characters(), sizeof(__getserv_protocol_buffer));
} else { } else {
perror("malformed services file: protocol"); perror("malformed services file: protocol");
return false; return false;
@ -566,7 +566,7 @@ static bool fill_getproto_buffers(char* line, ssize_t read)
return false; return false;
} }
if (sizeof(__getproto_name_buffer) >= split_line[0].length() + 1) { if (sizeof(__getproto_name_buffer) >= split_line[0].length() + 1) {
strncpy(__getproto_name_buffer, split_line[0].characters(), split_line[0].length() + 1); strlcpy(__getproto_name_buffer, split_line[0].characters(), sizeof(__getproto_name_buffer));
} else { } else {
perror("invalid buffer length: protocol name"); perror("invalid buffer length: protocol name");
return false; return false;

View file

@ -146,11 +146,11 @@ next_entry:
__pwdb_entry->pw_dir = __pwdb_entry->dir_buffer; __pwdb_entry->pw_dir = __pwdb_entry->dir_buffer;
__pwdb_entry->pw_shell = __pwdb_entry->shell_buffer; __pwdb_entry->pw_shell = __pwdb_entry->shell_buffer;
strncpy(__pwdb_entry->name_buffer, e_name.characters(), PWDB_STR_MAX_LEN - 1); strlcpy(__pwdb_entry->name_buffer, e_name.characters(), PWDB_STR_MAX_LEN);
strncpy(__pwdb_entry->passwd_buffer, e_passwd.characters(), PWDB_STR_MAX_LEN - 1); strlcpy(__pwdb_entry->passwd_buffer, e_passwd.characters(), PWDB_STR_MAX_LEN);
strncpy(__pwdb_entry->gecos_buffer, e_gecos.characters(), PWDB_STR_MAX_LEN - 1); strlcpy(__pwdb_entry->gecos_buffer, e_gecos.characters(), PWDB_STR_MAX_LEN);
strncpy(__pwdb_entry->dir_buffer, e_dir.characters(), PWDB_STR_MAX_LEN - 1); strlcpy(__pwdb_entry->dir_buffer, e_dir.characters(), PWDB_STR_MAX_LEN);
strncpy(__pwdb_entry->shell_buffer, e_shell.characters(), PWDB_STR_MAX_LEN - 1); strlcpy(__pwdb_entry->shell_buffer, e_shell.characters(), PWDB_STR_MAX_LEN);
return __pwdb_entry; return __pwdb_entry;
} }

View file

@ -554,7 +554,7 @@ char* getlogin()
{ {
static char __getlogin_buffer[256]; static char __getlogin_buffer[256];
if (auto* passwd = getpwuid(getuid())) { if (auto* passwd = getpwuid(getuid())) {
strncpy(__getlogin_buffer, passwd->pw_name, sizeof(__getlogin_buffer) - 1); strlcpy(__getlogin_buffer, passwd->pw_name, sizeof(__getlogin_buffer));
endpwent(); endpwent();
return __getlogin_buffer; return __getlogin_buffer;
} }