From 1924e4e0c9c863b641f08224f740feb51bf1bc7d Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Thu, 15 Oct 2026 12:28:35 +0300 Subject: Document snapshot pointer lifetimes and make getters NULL-safe The snapshot getters crashed on a NULL snapshot, unlike the rest of the API. They now return NULL, an empty span, 0, or write nothing, as the terminal functions do. term_snapshot_cluster writes nothing when cps is NULL or cap is 0. Document that the view and its palette keep their address for the life of the snapshot, and that a row pointer lasts only until the next update or free, since an update can realloc the cell array. Rename the term_input.h include guard to TERM_INPUT_H to match the file. Co-Authored-By: Claude Haiku 5.5 --- src/snapshot.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/snapshot.c b/src/snapshot.c index 8dbb498..c578f18 100644 --- a/src/snapshot.c +++ b/src/snapshot.c @@ -314,34 +314,36 @@ bool term_snapshot_update(struct term_snapshot *s, struct term *t) const struct term_view *term_snapshot_view(const struct term_snapshot *s) { - return &s->view; + return s ? &s->view : NULL; } const struct term_cell *term_snapshot_row(const struct term_snapshot *s, int y) { - if (y < 0 || y >= s->view.size.y) + if (!s || y < 0 || y >= s->view.size.y) return NULL; return s->cells + (size_t)y * s->view.size.x; } uint64_t term_snapshot_row_key(const struct term_snapshot *s, int y) { - if (y < 0 || y >= s->view.size.y) + if (!s || y < 0 || y >= s->view.size.y) return 0; return s->keys[y]; } struct term_span term_snapshot_row_selection(const struct term_snapshot *s, int y) { - if (y < 0 || y >= s->view.size.y) + if (!s || y < 0 || y >= s->view.size.y) return (struct term_span){ 0 }; return s->selection[y]; } int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t *cps, int cap) { + if (!s) + return 0; if (!(text & TERM_TEXT_CLUSTER)) { - if (cap > 0) + if (cps && cap > 0) cps[0] = text; return 1; } @@ -350,7 +352,7 @@ int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t return 0; int n = (int)s->arena[off]; int copy = n < cap ? n : cap; - if (copy > 0) + if (cps && copy > 0) memcpy(cps, &s->arena[off + 1], (size_t)copy * sizeof(*cps)); return n; } -- cgit v1.2.3