dix: rpcbuf: flag for auto-clear in error case

when the err_clear flag is set, the buffer memory will automatically
be free()ed when allocation failed. This allows simplificatoin of
caller's error pathes: the caller doesn't need to to call x_rpcbuf_clear()
anymore and eg. can directly return out.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult
2025-07-17 14:58:45 +02:00
parent d90620f363
commit 4efe7768f1
2 changed files with 14 additions and 8 deletions

View File

@@ -16,10 +16,8 @@ Bool x_rpcbuf_makeroom(struct x_rpcbuf *rpcbuf, size_t needed)
/* not allocated yet ? */
if (!rpcbuf->buffer) {
if (!(rpcbuf->buffer = calloc(1, XLIBRE_RPCBUF_CHUNK_SIZE))) {
rpcbuf->error = TRUE;
return FALSE;
}
if (!(rpcbuf->buffer = calloc(1, XLIBRE_RPCBUF_CHUNK_SIZE)))
goto err;
rpcbuf->size = XLIBRE_RPCBUF_CHUNK_SIZE;
rpcbuf->wpos = 0;
}
@@ -32,15 +30,22 @@ Bool x_rpcbuf_makeroom(struct x_rpcbuf *rpcbuf, size_t needed)
* XLIBRE_RPCBUF_CHUNK_SIZE;
char *newbuf = realloc(rpcbuf->buffer, newsize);
if (!newbuf) {
rpcbuf->error = TRUE;
return FALSE;
}
if (!newbuf)
goto err;
memset(newbuf + rpcbuf->size, 0, newsize - rpcbuf->size);
rpcbuf->buffer = newbuf;
rpcbuf->size = newsize;
return TRUE;
err:
rpcbuf->error = TRUE;
if (rpcbuf->err_clear) {
free(rpcbuf->buffer);
rpcbuf->buffer = NULL;
}
return FALSE;
}
void x_rpcbuf_clear(struct x_rpcbuf *rpcbuf)

View File

@@ -32,6 +32,7 @@ typedef struct x_rpcbuf {
char *buffer; /* pointer to whole buffer */
Bool swapped; /* TRUE when typed write operation shall byte-swap */
Bool error; /* TRUE when the last allocation failed */
Bool err_clear; /* set to TRUE if should automatically clear on error */
} x_rpcbuf_t;
#define XLIBRE_RPCBUF_CHUNK_SIZE 4096