Append a section to the render buffer. * * snprintf returns the length it WOULD have written, so a section larger than * the space left would otherwise push pos past buf_sz; the next call would then * compute a wrapped (huge) remaining size from (buf_sz - pos) and write out of * bounds. Clamp pos into [0, buf_sz-1] after every write so each snprintf gets * a positive size and the returned cu
| 7899 | * bounds. Clamp pos into [0, buf_sz-1] after every write so each snprintf gets |
| 7900 | * a positive size and the returned cursor never escapes the buffer. */ |
| 7901 | static int adr_render_section(char *buf, int buf_sz, int pos, const char *key, const char *value) { |
| 7902 | if (buf_sz <= 0) { |
| 7903 | return 0; |
| 7904 | } |
| 7905 | if (pos < 0) { |
| 7906 | pos = 0; |
| 7907 | } |
| 7908 | if (pos >= buf_sz) { |
| 7909 | return buf_sz - SKIP_ONE; |
| 7910 | } |
| 7911 | if (pos > 0) { |
| 7912 | pos += snprintf(buf + pos, (size_t)(buf_sz - pos), "\n\n"); |
| 7913 | if (pos >= buf_sz) { |
| 7914 | return buf_sz - SKIP_ONE; |
| 7915 | } |
| 7916 | } |
| 7917 | pos += snprintf(buf + pos, (size_t)(buf_sz - pos), "## %s\n%s", key, value); |
| 7918 | if (pos >= buf_sz) { |
| 7919 | pos = buf_sz - SKIP_ONE; |
| 7920 | } |
| 7921 | return pos; |
| 7922 | } |
| 7923 | |
| 7924 | char *cbm_adr_render(const cbm_adr_sections_t *sections) { |
| 7925 | if (!sections || sections->count == 0) { |