From 00d57d783ea5252a8e7fd129241759308019f7c7 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 10 Dec 2023 19:20:14 +0200 Subject: [PATCH] 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. --- libc/CMakeLists.txt | 1 + libc/pwd.cpp | 4 ++-- libc/stdio.cpp | 4 ++-- userspace/Shell/main.cpp | 3 +-- userspace/aoc2023/day10/main.cpp | 13 +++++++------ userspace/aoc2023/day2/main.cpp | 2 +- userspace/aoc2023/day3/main.cpp | 16 ++++++++-------- userspace/aoc2023/day5/main.cpp | 9 +++++---- userspace/aoc2023/day6/main.cpp | 5 +++-- userspace/aoc2023/day7/main.cpp | 5 +++-- userspace/aoc2023/day8/main.cpp | 5 +++-- userspace/aoc2023/day9/main.cpp | 5 +++-- userspace/cat/main.cpp | 1 - userspace/cp/main.cpp | 2 +- userspace/dd/main.cpp | 6 +----- userspace/image/Netbpm.cpp | 3 ++- userspace/ls/main.cpp | 20 ++++++++++---------- userspace/poweroff/main.cpp | 2 +- userspace/rm/main.cpp | 4 ++-- userspace/snake/main.cpp | 6 +++++- userspace/stat/main.cpp | 2 +- userspace/u8sum/main.cpp | 2 +- 22 files changed, 63 insertions(+), 57 deletions(-) diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 82081493..8f12027c 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -47,6 +47,7 @@ add_library(libc ${LIBC_SOURCES}) add_dependencies(libc headers crtx-install) 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 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/libc.a ${BANAN_LIB}/ diff --git a/libc/pwd.cpp b/libc/pwd.cpp index 05a8c3be..4cf53fa1 100644 --- a/libc/pwd.cpp +++ b/libc/pwd.cpp @@ -104,7 +104,7 @@ struct passwd* getpwnam(const char* name) { passwd* pwd; setpwent(); - while (pwd = getpwent()) + while ((pwd = getpwent())) if (strcmp(pwd->pw_name, name) == 0) return pwd; return nullptr; @@ -114,7 +114,7 @@ struct passwd* getpwuid(uid_t uid) { passwd* pwd; setpwent(); - while (pwd = getpwent()) + while ((pwd = getpwent())) if (pwd->pw_uid == uid) return pwd; return nullptr; diff --git a/libc/stdio.cpp b/libc/stdio.cpp index 54f22045..63b00d93 100644 --- a/libc/stdio.cpp +++ b/libc/stdio.cpp @@ -13,7 +13,7 @@ struct FILE bool eof { false }; bool error { false }; - unsigned char buffer[BUFSIZ]; + unsigned char buffer[BUFSIZ] {}; 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) { - 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) file->error = true; diff --git a/userspace/Shell/main.cpp b/userspace/Shell/main.cpp index c62a22d9..1433070d 100644 --- a/userspace/Shell/main.cpp +++ b/userspace/Shell/main.cpp @@ -600,7 +600,6 @@ int execute_piped_commands(BAN::Vector>& commands) int next_stdin = STDIN_FILENO; for (size_t i = 0; i < commands.size(); i++) { - bool first = (i == 0); bool last = (i == commands.size() - 1); int pipefd[2] { -1, STDOUT_FILENO }; @@ -831,7 +830,7 @@ int prompt_length() void print_prompt() { auto prompt = get_prompt(); - fprintf(stdout, "%.*s", prompt.size(), prompt.data()); + fprintf(stdout, "%.*s", (int)prompt.size(), prompt.data()); fflush(stdout); } diff --git a/userspace/aoc2023/day10/main.cpp b/userspace/aoc2023/day10/main.cpp index 0e3b58a7..175a0704 100644 --- a/userspace/aoc2023/day10/main.cpp +++ b/userspace/aoc2023/day10/main.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -85,13 +86,13 @@ BAN::Array find_grid_first_moves(const Grid& grid) if (grid[y][x] == 'S') { 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 }; - 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 }; - 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 }; - 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 }; ASSERT(index == 2); return positions; @@ -268,11 +269,11 @@ int main(int argc, char** argv) return 1; } - printf("puzzle1: %lld\n", puzzle1(fp)); + printf("puzzle1: %" PRId64 "\n", puzzle1(fp)); fseek(fp, 0, SEEK_SET); - printf("puzzle2: %lld\n", puzzle2(fp)); + printf("puzzle2: %" PRId64 "\n", puzzle2(fp)); fclose(fp); } diff --git a/userspace/aoc2023/day2/main.cpp b/userspace/aoc2023/day2/main.cpp index 957dd157..6ea627d0 100644 --- a/userspace/aoc2023/day2/main.cpp +++ b/userspace/aoc2023/day2/main.cpp @@ -64,7 +64,7 @@ int puzzle2(FILE* fp) continue; ptr += 5; - int id = parse_int_and_advance(ptr); + parse_int_and_advance(ptr); ptr += 2; int needed_red = 0; diff --git a/userspace/aoc2023/day3/main.cpp b/userspace/aoc2023/day3/main.cpp index 7e96bc52..2f65b471 100644 --- a/userspace/aoc2023/day3/main.cpp +++ b/userspace/aoc2023/day3/main.cpp @@ -21,9 +21,9 @@ int puzzle1(FILE* fp) 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])) continue; @@ -32,14 +32,14 @@ int puzzle1(FILE* fp) for (ssize_t y_off = -1; y_off <= 1; y_off++) { - if (y + y_off < 0) + if ((ssize_t)y < y_off) continue; if (y + y_off >= lines.size()) break; for (ssize_t x_off = -1;; x_off++) { - if (x + x_off < 0) + if ((ssize_t)x < x_off) continue; if (x + x_off >= lines[y + y_off].size()) break; @@ -94,9 +94,9 @@ int puzzle2(FILE* fp) // Map numbers next to '*' to asterisk's coordinates. HashMap> 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])) continue; @@ -111,14 +111,14 @@ int puzzle2(FILE* fp) for (ssize_t y_off = -1; y_off <= 1; y_off++) { - if (y + y_off < 0) + if ((ssize_t)y < y_off) continue; if (y + y_off >= lines.size()) break; for (ssize_t x_off = -1;; x_off++) { - if (x + x_off < 0) + if ((ssize_t)x < x_off) continue; if (x + x_off >= lines[y + y_off].size()) break; diff --git a/userspace/aoc2023/day5/main.cpp b/userspace/aoc2023/day5/main.cpp index f819fb9e..8e33608c 100644 --- a/userspace/aoc2023/day5/main.cpp +++ b/userspace/aoc2023/day5/main.cpp @@ -1,6 +1,7 @@ #include #include +#include #include using i64 = int64_t; @@ -31,7 +32,7 @@ i64 puzzle1(FILE* fp) line = line.substring(0, line.size() - 1); 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)); } @@ -92,7 +93,7 @@ i64 puzzle2(FILE* fp) BAN::StringView line(buffer); line = line.substring(0, line.size() - 1); 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)); } @@ -179,11 +180,11 @@ int main(int argc, char** argv) return 1; } - printf("puzzle1: %lld\n", puzzle1(fp)); + printf("puzzle1: %" PRId64 "\n", puzzle1(fp)); fseek(fp, 0, SEEK_SET); - printf("puzzle2: %lld\n", puzzle2(fp)); + printf("puzzle2: %" PRId64 "\n", puzzle2(fp)); fclose(fp); } diff --git a/userspace/aoc2023/day6/main.cpp b/userspace/aoc2023/day6/main.cpp index b857fc51..a56d62c3 100644 --- a/userspace/aoc2023/day6/main.cpp +++ b/userspace/aoc2023/day6/main.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -112,11 +113,11 @@ int main(int argc, char** argv) return 1; } - printf("puzzle1: %lld\n", puzzle1(fp)); + printf("puzzle1: %" PRId64 "\n", puzzle1(fp)); fseek(fp, 0, SEEK_SET); - printf("puzzle2: %lld\n", puzzle2(fp)); + printf("puzzle1: %" PRId64 "\n", puzzle2(fp)); fclose(fp); } diff --git a/userspace/aoc2023/day7/main.cpp b/userspace/aoc2023/day7/main.cpp index b1dbdbb2..bcfebdc0 100644 --- a/userspace/aoc2023/day7/main.cpp +++ b/userspace/aoc2023/day7/main.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -142,11 +143,11 @@ int main(int argc, char** argv) return 1; } - printf("puzzle1: %lld\n", puzzle(fp, false)); + printf("puzzle1: %" PRId64 "\n", puzzle(fp, false)); fseek(fp, 0, SEEK_SET); - printf("puzzle2: %lld\n", puzzle(fp, true)); + printf("puzzle2: %" PRId64 "\n", puzzle(fp, true)); fclose(fp); } diff --git a/userspace/aoc2023/day8/main.cpp b/userspace/aoc2023/day8/main.cpp index 4ec0410d..a4be9173 100644 --- a/userspace/aoc2023/day8/main.cpp +++ b/userspace/aoc2023/day8/main.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -121,11 +122,11 @@ int main(int argc, char** argv) return 1; } - printf("puzzle1: %lld\n", puzzle1(fp)); + printf("puzzle1: %" PRId64 "\n", puzzle1(fp)); fseek(fp, 0, SEEK_SET); - printf("puzzle2: %lld\n", puzzle2(fp)); + printf("puzzle2: %" PRId64 "\n", puzzle2(fp)); fclose(fp); } diff --git a/userspace/aoc2023/day9/main.cpp b/userspace/aoc2023/day9/main.cpp index a23bc44a..d5637203 100644 --- a/userspace/aoc2023/day9/main.cpp +++ b/userspace/aoc2023/day9/main.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -114,11 +115,11 @@ int main(int argc, char** argv) return 1; } - printf("puzzle1: %lld\n", puzzle1(fp)); + printf("puzzle1: %" PRId64 "\n", puzzle1(fp)); fseek(fp, 0, SEEK_SET); - printf("puzzle2: %lld\n", puzzle2(fp)); + printf("puzzle2: %" PRId64 "\n", puzzle2(fp)); fclose(fp); } diff --git a/userspace/cat/main.cpp b/userspace/cat/main.cpp index 28b43095..cd367580 100644 --- a/userspace/cat/main.cpp +++ b/userspace/cat/main.cpp @@ -4,7 +4,6 @@ bool cat_file(int fd) { char buffer[1024]; - size_t n_read; while (ssize_t n_read = read(fd, buffer, sizeof(buffer))) { if (n_read == -1) diff --git a/userspace/cp/main.cpp b/userspace/cp/main.cpp index 58d8a5c0..e3d3acdc 100644 --- a/userspace/cp/main.cpp +++ b/userspace/cp/main.cpp @@ -59,7 +59,7 @@ bool copy_file(const BAN::String& source, BAN::String destination) break; } - size_t written = 0; + ssize_t written = 0; while (written < nread) { ssize_t nwrite = write(dest_fd, buffer, nread - written); diff --git a/userspace/dd/main.cpp b/userspace/dd/main.cpp index 0b574b21..0d105676 100644 --- a/userspace/dd/main.cpp +++ b/userspace/dd/main.cpp @@ -10,9 +10,7 @@ int parse_int(const char* val) { int result = 0; - - const char* ptr = val; - while (*ptr) + for (const char* ptr = val; *ptr; ptr++) { if (!isdigit(*ptr)) { @@ -20,9 +18,7 @@ int parse_int(const char* val) exit(1); } result = (result * 10) + (*ptr - '0'); - *ptr++; } - return result; } diff --git a/userspace/image/Netbpm.cpp b/userspace/image/Netbpm.cpp index 5f424882..93cd69bf 100644 --- a/userspace/image/Netbpm.cpp +++ b/userspace/image/Netbpm.cpp @@ -3,6 +3,7 @@ #include #include +#include #include BAN::Optional parse_u64(const uint8_t*& data, size_t data_size) @@ -86,7 +87,7 @@ BAN::ErrorOr> load_netbpm(const void* mmap_addr, size_t size 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 bitmap; TRY(bitmap.resize(*width * *height)); diff --git a/userspace/ls/main.cpp b/userspace/ls/main.cpp index 9a3c42ec..f060a159 100644 --- a/userspace/ls/main.cpp +++ b/userspace/ls/main.cpp @@ -138,7 +138,7 @@ int list_directory(const BAN::String& path, config_t config) } struct dirent* dirent; - while (dirent = readdir(dirp)) + while ((dirent = readdir(dirp))) { if (!config.all && dirent->d_name[0] == '.') continue; @@ -216,15 +216,15 @@ int list_directory(const BAN::String& path, config_t config) for (const auto& full_entry : full_entries) printf("%*s %*s %*s %*s %*s %*s %*s %*s %s\n", - max_entry.access.size(), full_entry.access.data(), - max_entry.hard_links.size(), full_entry.hard_links.data(), - max_entry.owner_name.size(), full_entry.owner_name.data(), - max_entry.owner_group.size(), full_entry.owner_group.data(), - max_entry.size.size(), full_entry.size.data(), - max_entry.month.size(), full_entry.month.data(), - max_entry.day.size(), full_entry.day.data(), - max_entry.time.size(), full_entry.time.data(), - full_entry.full_name.data() + (int)max_entry.access.size(), full_entry.access.data(), + (int)max_entry.hard_links.size(), full_entry.hard_links.data(), + (int)max_entry.owner_name.size(), full_entry.owner_name.data(), + (int)max_entry.owner_group.size(), full_entry.owner_group.data(), + (int)max_entry.size.size(), full_entry.size.data(), + (int)max_entry.month.size(), full_entry.month.data(), + (int)max_entry.day.size(), full_entry.day.data(), + (int)max_entry.time.size(), full_entry.time.data(), + full_entry.full_name.data() ); return ret; diff --git a/userspace/poweroff/main.cpp b/userspace/poweroff/main.cpp index 797281bf..197cbd02 100644 --- a/userspace/poweroff/main.cpp +++ b/userspace/poweroff/main.cpp @@ -6,7 +6,7 @@ void usage(int ret, char* arg0) { 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, " -r, --reboot Reboot the system\n"); fprintf(fout, " -h, --help Show this message\n"); diff --git a/userspace/rm/main.cpp b/userspace/rm/main.cpp index 664fccc1..d42e8bcc 100644 --- a/userspace/rm/main.cpp +++ b/userspace/rm/main.cpp @@ -54,13 +54,13 @@ bool delete_recursive(const char* path) return false; } - return true; + return ret; } void usage(const char* argv0, int ret) { 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, "OPTIONS:\n"); fprintf(out, " -r remove directories and their contents recursively\n"); diff --git a/userspace/snake/main.cpp b/userspace/snake/main.cpp index bb532be0..b289a731 100644 --- a/userspace/snake/main.cpp +++ b/userspace/snake/main.cpp @@ -27,7 +27,7 @@ bool g_running = true; Point g_grid_size = { 21, 21 }; Direction g_direction = Direction::Up; Point g_head = { 10, 10 }; -int g_tail_target = 3; +size_t g_tail_target = 3; int g_score = 0; BAN::Vector g_tail; Point g_apple; @@ -133,6 +133,8 @@ void update() if (g_direction != Direction::Left) new_direction = Direction::Right; break; + default: + break; } } @@ -154,6 +156,8 @@ void update() case Direction::Right: g_head.x++; 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) diff --git a/userspace/stat/main.cpp b/userspace/stat/main.cpp index af230e68..a24265e7 100644 --- a/userspace/stat/main.cpp +++ b/userspace/stat/main.cpp @@ -7,7 +7,7 @@ void print_timestamp(timespec ts) { 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.hour, time.minute, time.second, ts.tv_nsec diff --git a/userspace/u8sum/main.cpp b/userspace/u8sum/main.cpp index 7cc2b8ce..db15c8f2 100644 --- a/userspace/u8sum/main.cpp +++ b/userspace/u8sum/main.cpp @@ -16,7 +16,7 @@ int main(int argc, char** argv) uint8_t buffer[1024]; 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]; if (ferror(fp))