LibC+userspace: Make everything compile with -Wall -Wextra -Werror

I added -Wall -Wextra -Werror as public compile flags to libc. Now
everything in userspace in compiled using these flags. I made all
necessary changes to allow compilation to work.

Only exception is execvp which has a large stack usage. Maybe it
should use malloc for the buffer but posix allows ENOMEM only when
kernel is out of memory... This can be fixed when fexecve is
implemented and there is no need for absolute path.
This commit is contained in:
Bananymous 2023-12-10 19:20:14 +02:00
parent f077e17b2a
commit 00d57d783e
22 changed files with 63 additions and 57 deletions

View File

@ -47,6 +47,7 @@ add_library(libc ${LIBC_SOURCES})
add_dependencies(libc headers crtx-install) add_dependencies(libc headers crtx-install)
target_compile_options(libc PRIVATE -g -Wstack-usage=512) target_compile_options(libc PRIVATE -g -Wstack-usage=512)
target_compile_options(libc PUBLIC -Wall -Wextra -Werror -Wno-error=stack-usage=)
add_custom_target(libc-install add_custom_target(libc-install
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/libc.a ${BANAN_LIB}/ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/libc.a ${BANAN_LIB}/

View File

@ -104,7 +104,7 @@ struct passwd* getpwnam(const char* name)
{ {
passwd* pwd; passwd* pwd;
setpwent(); setpwent();
while (pwd = getpwent()) while ((pwd = getpwent()))
if (strcmp(pwd->pw_name, name) == 0) if (strcmp(pwd->pw_name, name) == 0)
return pwd; return pwd;
return nullptr; return nullptr;
@ -114,7 +114,7 @@ struct passwd* getpwuid(uid_t uid)
{ {
passwd* pwd; passwd* pwd;
setpwent(); setpwent();
while (pwd = getpwent()) while ((pwd = getpwent()))
if (pwd->pw_uid == uid) if (pwd->pw_uid == uid)
return pwd; return pwd;
return nullptr; return nullptr;

View File

@ -13,7 +13,7 @@ struct FILE
bool eof { false }; bool eof { false };
bool error { false }; bool error { false };
unsigned char buffer[BUFSIZ]; unsigned char buffer[BUFSIZ] {};
uint32_t buffer_index { 0 }; uint32_t buffer_index { 0 };
}; };
@ -260,7 +260,7 @@ size_t fread(void* buffer, size_t size, size_t nitems, FILE* file)
while (nread < target) while (nread < target)
{ {
size_t ret = syscall(SYS_READ, file->fd, (uint8_t*)buffer + nread, target - nread); ssize_t ret = syscall(SYS_READ, file->fd, (uint8_t*)buffer + nread, target - nread);
if (ret < 0) if (ret < 0)
file->error = true; file->error = true;

View File

@ -600,7 +600,6 @@ int execute_piped_commands(BAN::Vector<BAN::Vector<BAN::String>>& commands)
int next_stdin = STDIN_FILENO; int next_stdin = STDIN_FILENO;
for (size_t i = 0; i < commands.size(); i++) for (size_t i = 0; i < commands.size(); i++)
{ {
bool first = (i == 0);
bool last = (i == commands.size() - 1); bool last = (i == commands.size() - 1);
int pipefd[2] { -1, STDOUT_FILENO }; int pipefd[2] { -1, STDOUT_FILENO };
@ -831,7 +830,7 @@ int prompt_length()
void print_prompt() void print_prompt()
{ {
auto prompt = get_prompt(); auto prompt = get_prompt();
fprintf(stdout, "%.*s", prompt.size(), prompt.data()); fprintf(stdout, "%.*s", (int)prompt.size(), prompt.data());
fflush(stdout); fflush(stdout);
} }

View File

@ -3,6 +3,7 @@
#include <BAN/Vector.h> #include <BAN/Vector.h>
#include <ctype.h> #include <ctype.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
@ -85,13 +86,13 @@ BAN::Array<Position, 2> find_grid_first_moves(const Grid& grid)
if (grid[y][x] == 'S') if (grid[y][x] == 'S')
{ {
size_t index = 0; size_t index = 0;
if (can_enter_tile_from(grid[y - 1][x], Direction::South)) if (index < 2 && can_enter_tile_from(grid[y - 1][x], Direction::South))
positions[index++] = { x, y - 1, Direction::South }; positions[index++] = { x, y - 1, Direction::South };
if (can_enter_tile_from(grid[y + 1][x], Direction::North)) if (index < 2 && can_enter_tile_from(grid[y + 1][x], Direction::North))
positions[index++] = { x, y + 1, Direction::North }; positions[index++] = { x, y + 1, Direction::North };
if (can_enter_tile_from(grid[y][x - 1], Direction::East)) if (index < 2 && can_enter_tile_from(grid[y][x - 1], Direction::East))
positions[index++] = { x - 1, y, Direction::East }; positions[index++] = { x - 1, y, Direction::East };
if (can_enter_tile_from(grid[y][x + 1], Direction::West)) if (index < 2 && can_enter_tile_from(grid[y][x + 1], Direction::West))
positions[index++] = { x + 1, y, Direction::West }; positions[index++] = { x + 1, y, Direction::West };
ASSERT(index == 2); ASSERT(index == 2);
return positions; return positions;
@ -268,11 +269,11 @@ int main(int argc, char** argv)
return 1; return 1;
} }
printf("puzzle1: %lld\n", puzzle1(fp)); printf("puzzle1: %" PRId64 "\n", puzzle1(fp));
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
printf("puzzle2: %lld\n", puzzle2(fp)); printf("puzzle2: %" PRId64 "\n", puzzle2(fp));
fclose(fp); fclose(fp);
} }

View File

@ -64,7 +64,7 @@ int puzzle2(FILE* fp)
continue; continue;
ptr += 5; ptr += 5;
int id = parse_int_and_advance(ptr); parse_int_and_advance(ptr);
ptr += 2; ptr += 2;
int needed_red = 0; int needed_red = 0;

View File

@ -21,9 +21,9 @@ int puzzle1(FILE* fp)
int result = 0; int result = 0;
for (ssize_t y = 0; y < lines.size(); y++) for (size_t y = 0; y < lines.size(); y++)
{ {
for (ssize_t x = 0; x < lines[y].size(); x++) for (size_t x = 0; x < lines[y].size(); x++)
{ {
if (!isdigit(lines[y][x])) if (!isdigit(lines[y][x]))
continue; continue;
@ -32,14 +32,14 @@ int puzzle1(FILE* fp)
for (ssize_t y_off = -1; y_off <= 1; y_off++) for (ssize_t y_off = -1; y_off <= 1; y_off++)
{ {
if (y + y_off < 0) if ((ssize_t)y < y_off)
continue; continue;
if (y + y_off >= lines.size()) if (y + y_off >= lines.size())
break; break;
for (ssize_t x_off = -1;; x_off++) for (ssize_t x_off = -1;; x_off++)
{ {
if (x + x_off < 0) if ((ssize_t)x < x_off)
continue; continue;
if (x + x_off >= lines[y + y_off].size()) if (x + x_off >= lines[y + y_off].size())
break; break;
@ -94,9 +94,9 @@ int puzzle2(FILE* fp)
// Map numbers next to '*' to asterisk's coordinates. // Map numbers next to '*' to asterisk's coordinates.
HashMap<uint32_t, Vector<int>> gears; HashMap<uint32_t, Vector<int>> gears;
for (ssize_t y = 0; y < lines.size(); y++) for (size_t y = 0; y < lines.size(); y++)
{ {
for (ssize_t x = 0; x < lines[y].size(); x++) for (size_t x = 0; x < lines[y].size(); x++)
{ {
if (!isdigit(lines[y][x])) if (!isdigit(lines[y][x]))
continue; continue;
@ -111,14 +111,14 @@ int puzzle2(FILE* fp)
for (ssize_t y_off = -1; y_off <= 1; y_off++) for (ssize_t y_off = -1; y_off <= 1; y_off++)
{ {
if (y + y_off < 0) if ((ssize_t)y < y_off)
continue; continue;
if (y + y_off >= lines.size()) if (y + y_off >= lines.size())
break; break;
for (ssize_t x_off = -1;; x_off++) for (ssize_t x_off = -1;; x_off++)
{ {
if (x + x_off < 0) if ((ssize_t)x < x_off)
continue; continue;
if (x + x_off >= lines[y + y_off].size()) if (x + x_off >= lines[y + y_off].size())
break; break;

View File

@ -1,6 +1,7 @@
#include <BAN/HashMap.h> #include <BAN/HashMap.h>
#include <BAN/Vector.h> #include <BAN/Vector.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
using i64 = int64_t; using i64 = int64_t;
@ -31,7 +32,7 @@ i64 puzzle1(FILE* fp)
line = line.substring(0, line.size() - 1); line = line.substring(0, line.size() - 1);
auto seeds_str = MUST(line.split(' ')); auto seeds_str = MUST(line.split(' '));
for (i64 i = 1; i < seeds_str.size(); i++) for (size_t i = 1; i < seeds_str.size(); i++)
MUST(current.emplace_back(parse_i64(seeds_str[i]), 0)); MUST(current.emplace_back(parse_i64(seeds_str[i]), 0));
} }
@ -92,7 +93,7 @@ i64 puzzle2(FILE* fp)
BAN::StringView line(buffer); BAN::StringView line(buffer);
line = line.substring(0, line.size() - 1); line = line.substring(0, line.size() - 1);
auto seeds_str = MUST(line.split(' ')); auto seeds_str = MUST(line.split(' '));
for (i64 i = 1; i < seeds_str.size(); i += 2) for (size_t i = 1; i < seeds_str.size(); i += 2)
MUST(current.emplace_back(parse_i64(seeds_str[i]), parse_i64(seeds_str[i + 1]), 0)); MUST(current.emplace_back(parse_i64(seeds_str[i]), parse_i64(seeds_str[i + 1]), 0));
} }
@ -179,11 +180,11 @@ int main(int argc, char** argv)
return 1; return 1;
} }
printf("puzzle1: %lld\n", puzzle1(fp)); printf("puzzle1: %" PRId64 "\n", puzzle1(fp));
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
printf("puzzle2: %lld\n", puzzle2(fp)); printf("puzzle2: %" PRId64 "\n", puzzle2(fp));
fclose(fp); fclose(fp);
} }

View File

@ -1,6 +1,7 @@
#include <BAN/Vector.h> #include <BAN/Vector.h>
#include <ctype.h> #include <ctype.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
@ -112,11 +113,11 @@ int main(int argc, char** argv)
return 1; return 1;
} }
printf("puzzle1: %lld\n", puzzle1(fp)); printf("puzzle1: %" PRId64 "\n", puzzle1(fp));
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
printf("puzzle2: %lld\n", puzzle2(fp)); printf("puzzle1: %" PRId64 "\n", puzzle2(fp));
fclose(fp); fclose(fp);
} }

View File

@ -3,6 +3,7 @@
#include <BAN/String.h> #include <BAN/String.h>
#include <ctype.h> #include <ctype.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
@ -142,11 +143,11 @@ int main(int argc, char** argv)
return 1; return 1;
} }
printf("puzzle1: %lld\n", puzzle(fp, false)); printf("puzzle1: %" PRId64 "\n", puzzle(fp, false));
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
printf("puzzle2: %lld\n", puzzle(fp, true)); printf("puzzle2: %" PRId64 "\n", puzzle(fp, true));
fclose(fp); fclose(fp);
} }

View File

@ -2,6 +2,7 @@
#include <BAN/String.h> #include <BAN/String.h>
#include <ctype.h> #include <ctype.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
@ -121,11 +122,11 @@ int main(int argc, char** argv)
return 1; return 1;
} }
printf("puzzle1: %lld\n", puzzle1(fp)); printf("puzzle1: %" PRId64 "\n", puzzle1(fp));
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
printf("puzzle2: %lld\n", puzzle2(fp)); printf("puzzle2: %" PRId64 "\n", puzzle2(fp));
fclose(fp); fclose(fp);
} }

View File

@ -2,6 +2,7 @@
#include <BAN/Vector.h> #include <BAN/Vector.h>
#include <ctype.h> #include <ctype.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
@ -114,11 +115,11 @@ int main(int argc, char** argv)
return 1; return 1;
} }
printf("puzzle1: %lld\n", puzzle1(fp)); printf("puzzle1: %" PRId64 "\n", puzzle1(fp));
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
printf("puzzle2: %lld\n", puzzle2(fp)); printf("puzzle2: %" PRId64 "\n", puzzle2(fp));
fclose(fp); fclose(fp);
} }

View File

@ -4,7 +4,6 @@
bool cat_file(int fd) bool cat_file(int fd)
{ {
char buffer[1024]; char buffer[1024];
size_t n_read;
while (ssize_t n_read = read(fd, buffer, sizeof(buffer))) while (ssize_t n_read = read(fd, buffer, sizeof(buffer)))
{ {
if (n_read == -1) if (n_read == -1)

View File

@ -59,7 +59,7 @@ bool copy_file(const BAN::String& source, BAN::String destination)
break; break;
} }
size_t written = 0; ssize_t written = 0;
while (written < nread) while (written < nread)
{ {
ssize_t nwrite = write(dest_fd, buffer, nread - written); ssize_t nwrite = write(dest_fd, buffer, nread - written);

View File

@ -10,9 +10,7 @@
int parse_int(const char* val) int parse_int(const char* val)
{ {
int result = 0; int result = 0;
for (const char* ptr = val; *ptr; ptr++)
const char* ptr = val;
while (*ptr)
{ {
if (!isdigit(*ptr)) if (!isdigit(*ptr))
{ {
@ -20,9 +18,7 @@ int parse_int(const char* val)
exit(1); exit(1);
} }
result = (result * 10) + (*ptr - '0'); result = (result * 10) + (*ptr - '0');
*ptr++;
} }
return result; return result;
} }

View File

@ -3,6 +3,7 @@
#include <BAN/Optional.h> #include <BAN/Optional.h>
#include <ctype.h> #include <ctype.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
BAN::Optional<uint64_t> parse_u64(const uint8_t*& data, size_t data_size) BAN::Optional<uint64_t> parse_u64(const uint8_t*& data, size_t data_size)
@ -86,7 +87,7 @@ BAN::ErrorOr<BAN::UniqPtr<Image>> load_netbpm(const void* mmap_addr, size_t size
return BAN::Error::from_errno(EINVAL); return BAN::Error::from_errno(EINVAL);
} }
printf("Netbpm image %llux%llu\n", *width, *height); printf("Netbpm image %" PRIuPTR "x%" PRIuPTR "\n", *width, *height);
BAN::Vector<Image::Color> bitmap; BAN::Vector<Image::Color> bitmap;
TRY(bitmap.resize(*width * *height)); TRY(bitmap.resize(*width * *height));

View File

@ -138,7 +138,7 @@ int list_directory(const BAN::String& path, config_t config)
} }
struct dirent* dirent; struct dirent* dirent;
while (dirent = readdir(dirp)) while ((dirent = readdir(dirp)))
{ {
if (!config.all && dirent->d_name[0] == '.') if (!config.all && dirent->d_name[0] == '.')
continue; continue;
@ -216,14 +216,14 @@ int list_directory(const BAN::String& path, config_t config)
for (const auto& full_entry : full_entries) for (const auto& full_entry : full_entries)
printf("%*s %*s %*s %*s %*s %*s %*s %*s %s\n", printf("%*s %*s %*s %*s %*s %*s %*s %*s %s\n",
max_entry.access.size(), full_entry.access.data(), (int)max_entry.access.size(), full_entry.access.data(),
max_entry.hard_links.size(), full_entry.hard_links.data(), (int)max_entry.hard_links.size(), full_entry.hard_links.data(),
max_entry.owner_name.size(), full_entry.owner_name.data(), (int)max_entry.owner_name.size(), full_entry.owner_name.data(),
max_entry.owner_group.size(), full_entry.owner_group.data(), (int)max_entry.owner_group.size(), full_entry.owner_group.data(),
max_entry.size.size(), full_entry.size.data(), (int)max_entry.size.size(), full_entry.size.data(),
max_entry.month.size(), full_entry.month.data(), (int)max_entry.month.size(), full_entry.month.data(),
max_entry.day.size(), full_entry.day.data(), (int)max_entry.day.size(), full_entry.day.data(),
max_entry.time.size(), full_entry.time.data(), (int)max_entry.time.size(), full_entry.time.data(),
full_entry.full_name.data() full_entry.full_name.data()
); );

View File

@ -6,7 +6,7 @@
void usage(int ret, char* arg0) void usage(int ret, char* arg0)
{ {
FILE* fout = (ret == 0) ? stdout : stderr; FILE* fout = (ret == 0) ? stdout : stderr;
fprintf(fout, "usage: %s [OPTIONS]...\n"); fprintf(fout, "usage: %s [OPTIONS]...\n", arg0);
fprintf(fout, " -s, --shutdown Shutdown the system (default)\n"); fprintf(fout, " -s, --shutdown Shutdown the system (default)\n");
fprintf(fout, " -r, --reboot Reboot the system\n"); fprintf(fout, " -r, --reboot Reboot the system\n");
fprintf(fout, " -h, --help Show this message\n"); fprintf(fout, " -h, --help Show this message\n");

View File

@ -54,13 +54,13 @@ bool delete_recursive(const char* path)
return false; return false;
} }
return true; return ret;
} }
void usage(const char* argv0, int ret) void usage(const char* argv0, int ret)
{ {
FILE* out = (ret == 0) ? stdout : stderr; FILE* out = (ret == 0) ? stdout : stderr;
fprintf(out, "usage: %s [OPTIONS]... FILE...\n"); fprintf(out, "usage: %s [OPTIONS]... FILE...\n", argv0);
fprintf(out, " remove each FILE\n"); fprintf(out, " remove each FILE\n");
fprintf(out, "OPTIONS:\n"); fprintf(out, "OPTIONS:\n");
fprintf(out, " -r remove directories and their contents recursively\n"); fprintf(out, " -r remove directories and their contents recursively\n");

View File

@ -27,7 +27,7 @@ bool g_running = true;
Point g_grid_size = { 21, 21 }; Point g_grid_size = { 21, 21 };
Direction g_direction = Direction::Up; Direction g_direction = Direction::Up;
Point g_head = { 10, 10 }; Point g_head = { 10, 10 };
int g_tail_target = 3; size_t g_tail_target = 3;
int g_score = 0; int g_score = 0;
BAN::Vector<Point> g_tail; BAN::Vector<Point> g_tail;
Point g_apple; Point g_apple;
@ -133,6 +133,8 @@ void update()
if (g_direction != Direction::Left) if (g_direction != Direction::Left)
new_direction = Direction::Right; new_direction = Direction::Right;
break; break;
default:
break;
} }
} }
@ -154,6 +156,8 @@ void update()
case Direction::Right: case Direction::Right:
g_head.x++; g_head.x++;
break; break;
default:
ASSERT_NOT_REACHED();
} }
if (g_head.x < 0 || g_head.y < 0 || g_head.x >= g_grid_size.x || g_head.y >= g_grid_size.y) if (g_head.x < 0 || g_head.y < 0 || g_head.x >= g_grid_size.x || g_head.y >= g_grid_size.y)

View File

@ -7,7 +7,7 @@
void print_timestamp(timespec ts) void print_timestamp(timespec ts)
{ {
auto time = BAN::from_unix_time(ts.tv_sec); auto time = BAN::from_unix_time(ts.tv_sec);
printf("%04d-%02d-%02d %02d:%02d:%02d.%09d", printf("%04d-%02d-%02d %02d:%02d:%02d.%09ld",
time.year, time.month, time.day, time.year, time.month, time.day,
time.hour, time.minute, time.second, time.hour, time.minute, time.second,
ts.tv_nsec ts.tv_nsec

View File

@ -16,7 +16,7 @@ int main(int argc, char** argv)
uint8_t buffer[1024]; uint8_t buffer[1024];
while (size_t ret = fread(buffer, 1, sizeof(buffer), fp)) while (size_t ret = fread(buffer, 1, sizeof(buffer), fp))
for (int j = 0; j < ret; j++) for (size_t j = 0; j < ret; j++)
sum += buffer[j]; sum += buffer[j];
if (ferror(fp)) if (ferror(fp))