diff --git a/libguac/include/guacio.h b/libguac/include/guacio.h index f0ee209d..9271c792 100644 --- a/libguac/include/guacio.h +++ b/libguac/include/guacio.h @@ -125,9 +125,13 @@ typedef struct GUACIO { * Allocates and initializes a new GUACIO object with the given open * file descriptor. * + * If an error occurs while allocating the GUACIO object, NULL is returned, + * and guac_error is set appropriately. + * * @param fd An open file descriptor that this GUACIO object should manage. * @return A newly allocated GUACIO object associated with the given - * file descriptor. + * file descriptor, or NULL if an error occurs while allocating + * the GUACIO object. */ GUACIO* guac_open(int fd); @@ -147,6 +151,9 @@ int64_t guac_parse_int(const char* str); * written may be buffered until the buffer is flushed automatically or * manually. * + * If an error occurs while writing, a non-zero value is returned, and + * guac_error is set appropriately. + * * @param io The GUACIO object to write to. * @param i The unsigned int to write. * @return Zero on success, or non-zero if an error occurs while writing. @@ -160,6 +167,9 @@ ssize_t guac_write_int(GUACIO* io, int64_t i); * internally by the Guacamole protocol (commas, semicolons, or * backslashes) it will need to be escaped. * + * If an error occurs while writing, a non-zero value is returned, and + * guac_error is set appropriately. + * * @param io The GUACIO object to write to. * @param str The string to write. * @return Zero on success, or non-zero if an error occurs while writing. @@ -174,6 +184,9 @@ ssize_t guac_write_string(GUACIO* io, const char* str); * be made before non-base64 writes (or writes of an independent block of * base64 data) can be made. * + * If an error occurs while writing, a non-zero value is returned, and + * guac_error is set appropriately. + * * @param io The GUACIO object to write to. * @param buf A buffer containing the data to write. * @param count The number of bytes to write. @@ -184,6 +197,9 @@ ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count); /** * Flushes the base64 buffer, writing padding characters as necessary. * + * If an error occurs while writing, a non-zero value is returned, and + * guac_error is set appropriately. + * * @param io The GUACIO object to flush * @return Zero on success, or non-zero if an error occurs during flush. */ @@ -192,6 +208,9 @@ ssize_t guac_flush_base64(GUACIO* io); /** * Flushes the write buffer. * + * If an error occurs while writing, a non-zero value is returned, and + * guac_error is set appropriately. + * * @param io The GUACIO object to flush * @return Zero on success, or non-zero if an error occurs during flush. */ diff --git a/libguac/src/guacio.c b/libguac/src/guacio.c index 4d2979da..b59a157c 100644 --- a/libguac/src/guacio.c +++ b/libguac/src/guacio.c @@ -53,6 +53,7 @@ #include #include "guacio.h" +#include "error.h" char __GUACIO_BASE64_CHARACTERS[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', @@ -64,6 +65,13 @@ char __GUACIO_BASE64_CHARACTERS[64] = { GUACIO* guac_open(int fd) { GUACIO* io = malloc(sizeof(GUACIO)); + + /* If no memory available, return with error */ + if (io == NULL) { + guac_error = GUAC_STATUS_NO_MEMORY; + return NULL; + } + io->ready = 0; io->written = 0; io->total_written = 0; @@ -72,6 +80,15 @@ GUACIO* guac_open(int fd) { /* Allocate instruction buffer */ io->instructionbuf_size = 1024; io->instructionbuf = malloc(io->instructionbuf_size); + + /* If no memory available, return with error */ + if (io->instructionbuf == NULL) { + guac_error = GUAC_STATUS_NO_MEMORY; + free(io); + return NULL; + } + + /* Init members */ io->instructionbuf_used_length = 0; io->instructionbuf_parse_start = 0; io->instructionbuf_elementc = 0; @@ -99,6 +116,10 @@ ssize_t __guac_write(GUACIO* io, const char* buf, int count) { retval = write(io->fd, buf, count); #endif + /* Record errors in guac_error */ + if (retval < 0) + guac_error = GUAC_STATUS_OUTPUT_ERROR; + return retval; }