fix: harden edge cases in message loading and network I/O

- Check ftell() return for errors (-1) in message_load to prevent
  corrupted backward scan on I/O failures
- Cap ssh_channel_write chunks to 32KB to prevent size_t-to-uint32_t
  narrowing on large buffers
- Log evicted active connection count in rate-limit table overflow
  warning for better diagnostics
This commit is contained in:
m1ngsama 2026-04-19 15:18:09 +08:00
parent 0de13a6314
commit 8be6476367
2 changed files with 8 additions and 4 deletions

View file

@ -61,7 +61,7 @@ int message_load(message_t **messages, int max_messages) {
} }
long file_size = ftell(fp); long file_size = ftell(fp);
if (file_size == 0) { if (file_size <= 0) {
fclose(fp); fclose(fp);
*messages = msg_array; *messages = msg_array;
return 0; return 0;

View file

@ -194,8 +194,10 @@ static ip_rate_limit_t* get_rate_limit_entry(const char *ip) {
oldest_idx = i; oldest_idx = i;
} }
} }
fprintf(stderr, "Warning: rate-limit table full, evicting active IP %s\n", fprintf(stderr, "Warning: rate-limit table full, evicting active IP %s "
g_rate_limits[oldest_idx].ip); "(%d active connections lost)\n",
g_rate_limits[oldest_idx].ip,
g_rate_limits[oldest_idx].active_connections);
} }
/* Reset and reuse */ /* Reset and reuse */
@ -487,7 +489,9 @@ int client_send(client_t *client, const char *data, size_t len) {
} }
while (total < len) { while (total < len) {
int sent = ssh_channel_write(client->channel, data + total, len - total); size_t remaining = len - total;
uint32_t chunk = (remaining > 32768) ? 32768 : (uint32_t)remaining;
int sent = ssh_channel_write(client->channel, data + total, chunk);
if (sent <= 0) { if (sent <= 0) {
pthread_mutex_unlock(&client->io_lock); pthread_mutex_unlock(&client->io_lock);
return -1; return -1;