From 4265167cb6e8c647e3a45439b30009a1df7de8a5 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Tue, 22 Jul 2025 10:48:57 +0200 Subject: [PATCH] ucode: add error reporting to pcap_write Return null on error, true if packets were written, false if no data available. Signed-off-by: Felix Fietkau --- lib-pcap.c | 7 +++++-- lib-ucode.c | 16 +++++++++++----- udebug-pcap.h | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib-pcap.c b/lib-pcap.c index 6340789..3293722 100644 --- a/lib-pcap.c +++ b/lib-pcap.c @@ -252,10 +252,13 @@ int pcap_snapshot_packet_init(struct udebug *ctx, struct udebug_iter *it) return 0; } -void pcap_block_write_file(FILE *f) +bool pcap_block_write_file(FILE *f) { - fwrite(pcap_buf, pcap_hdr->len, 1, f); + if (fwrite(pcap_buf, pcap_hdr->len, 1, f) != 1) + return false; + fflush(f); + return true; } void *pcap_block_get(size_t *len) diff --git a/lib-ucode.c b/lib-ucode.c index 92a7dbb..2b1d57f 100644 --- a/lib-ucode.c +++ b/lib-ucode.c @@ -351,6 +351,7 @@ uc_udebug_pcap_write(uc_vm_t *vm, size_t nargs) size_t n = ucv_type(arg) == UC_ARRAY ? ucv_array_length(arg) : 1; struct udebug_snapshot **s; struct udebug_iter it; + bool ret = false; if (!p) return NULL; @@ -359,10 +360,10 @@ uc_udebug_pcap_write(uc_vm_t *vm, size_t nargs) if (ucv_type(arg) == UC_ARRAY) for (size_t i = 0; i < n; i++) { if ((s[i] = uc_get_snapshot(ucv_array_get(arg, i))) == NULL) - return NULL; + goto out; } else { if ((s[0] = uc_get_snapshot(arg)) == NULL) - return NULL; + goto out; } udebug_iter_start(&it, s, n); @@ -374,16 +375,21 @@ uc_udebug_pcap_write(uc_vm_t *vm, size_t nargs) if (pcap_interface_rbuf_init(&p->pcap, rb)) continue; - pcap_block_write_file(p->f); + if (!pcap_block_write_file(p->f)) + return NULL; } if (pcap_snapshot_packet_init(&u, &it)) continue; - pcap_block_write_file(p->f); + if (!pcap_block_write_file(p->f)) + return NULL; + + ret = true; } - return NULL; +out: + return ucv_boolean_new(ret); } static void diff --git a/udebug-pcap.h b/udebug-pcap.h index 7c006b7..2ec8389 100644 --- a/udebug-pcap.h +++ b/udebug-pcap.h @@ -43,7 +43,7 @@ void pcap_packet_done(void); int pcap_interface_rbuf_init(struct pcap_context *p, struct udebug_remote_buf *rb); int pcap_snapshot_packet_init(struct udebug *ctx, struct udebug_iter *it); -void pcap_block_write_file(FILE *f); +bool pcap_block_write_file(FILE *f); void *pcap_block_get(size_t *len); #endif -- 2.30.2