Final code polish for consistency and correctness

- Use int32_t consistently in all loops
- Add zero-length check before memcpy
- Improve error detection in strtol (comment clarification)
- Ensure null terminator always has space reserved

Co-authored-by: squassina <8495707+squassina@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-02-18 09:04:20 +00:00
parent 969c68b66d
commit 1350b7787a
2 changed files with 8 additions and 4 deletions

View file

@ -607,10 +607,13 @@ static char *combine_args_until_empty(char *values[], int count) {
} }
if (remaining > 1) { if (remaining > 1) {
size_t val_len = strlen(values[i]); size_t val_len = strlen(values[i]);
// Always leave space for null terminator
size_t to_copy = (val_len < remaining - 1) ? val_len : remaining - 1; size_t to_copy = (val_len < remaining - 1) ? val_len : remaining - 1;
memcpy(ptr, values[i], to_copy); if (to_copy > 0) {
ptr += to_copy; memcpy(ptr, values[i], to_copy);
remaining -= to_copy; ptr += to_copy;
remaining -= to_copy;
}
} }
} }
*ptr = '\0'; // Null terminate *ptr = '\0'; // Null terminate
@ -648,6 +651,7 @@ uint32_t parse_mod(const char *mod_str) {
char *endptr; char *endptr;
errno = 0; errno = 0;
long keycode = strtol(token + 5, &endptr, 10); long keycode = strtol(token + 5, &endptr, 10);
// Check for conversion errors: overflow or no conversion
if (endptr != token + 5 && (*endptr == '\0' || *endptr == ' ') && errno != ERANGE) { if (endptr != token + 5 && (*endptr == '\0' || *endptr == ' ') && errno != ERANGE) {
switch (keycode) { switch (keycode) {
case 133: case 133:

View file

@ -865,7 +865,7 @@ int32_t spawn(const Arg *arg) {
// 4. execvp 失败时:清理并退出 // 4. execvp 失败时:清理并退出
// If execvp succeeds, this code never runs (process replaced) // If execvp succeeds, this code never runs (process replaced)
// If it fails, clean up allocated strings before exiting // If it fails, clean up allocated strings before exiting
for (int i = 0; i < alloc_count; i++) { for (int32_t i = 0; i < alloc_count; i++) {
free(allocated_strings[i]); free(allocated_strings[i]);
} }
wlr_log(WLR_ERROR, "mango: execvp '%s' failed: %s\n", argv[0], wlr_log(WLR_ERROR, "mango: execvp '%s' failed: %s\n", argv[0],