diff options
author | Lars-Dominik Braun <PromyLOPh@lavabit.com> | 2009-08-14 22:16:16 +0200 |
---|---|---|
committer | Lars-Dominik Braun <PromyLOPh@lavabit.com> | 2009-08-14 22:16:16 +0200 |
commit | f43699482a0a5921cb4cdc4bf110242278209ada (patch) | |
tree | ecef2ac0bbca45787e327c8fb8016ed393faae9b /libwaitress | |
parent | 0ca626237aef1c8ae30c31aa5ec2de09d2f31c42 (diff) | |
download | pianobar-f43699482a0a5921cb4cdc4bf110242278209ada.tar.gz pianobar-f43699482a0a5921cb4cdc4bf110242278209ada.tar.bz2 pianobar-f43699482a0a5921cb4cdc4bf110242278209ada.zip |
waitress: Use dynamic allocated buffer for FetchBuf
Diffstat (limited to 'libwaitress')
-rw-r--r-- | libwaitress/src/main.c | 46 | ||||
-rw-r--r-- | libwaitress/src/waitress.h | 2 |
2 files changed, 28 insertions, 20 deletions
diff --git a/libwaitress/src/main.c b/libwaitress/src/main.c index dbf1a74..cc06ec6 100644 --- a/libwaitress/src/main.c +++ b/libwaitress/src/main.c @@ -37,8 +37,7 @@ THE SOFTWARE. typedef struct { char *buf; - char *pos; - size_t max; + size_t pos; } WaitressFetchBufCbBuffer_t; inline void WaitressInit (WaitressHandle_t *waith) { @@ -178,40 +177,49 @@ inline void WaitressSetHPP (WaitressHandle_t *waith, const char *host, * @param data size * @param buffer structure */ -static char WaitressFetchBufCb (void *recvData, size_t recvDataSize, void *extraData) { +static char WaitressFetchBufCb (void *recvData, size_t recvDataSize, + void *extraData) { char *recvBytes = recvData; WaitressFetchBufCbBuffer_t *buffer = extraData; - if (buffer->pos - buffer->buf + recvDataSize < buffer->max) { - memcpy (buffer->pos, recvBytes, recvDataSize); - buffer->pos += recvDataSize; - *buffer->pos = '\0'; + if (buffer->buf == NULL) { + if ((buffer->buf = malloc (sizeof (*buffer->buf) * + (recvDataSize + 1))) == NULL) { + return 0; + } } else { - printf (PACKAGE ": Buffer overflow!\n"); - return 0; + char *newbuf; + if ((newbuf = realloc (buffer->buf, + sizeof (*buffer->buf) * + (buffer->pos + recvDataSize + 1))) == NULL) { + free (buffer->buf); + return 0; + } + buffer->buf = newbuf; } + memcpy (buffer->buf + buffer->pos, recvBytes, recvDataSize); + buffer->pos += recvDataSize; + *(buffer->buf+buffer->pos) = '\0'; return 1; } /* Fetch string. Beware! This overwrites your waith->data pointer * @param waitress handle - * @param Connect to host - * @param Port or service - * @param Resource path - * @param result buffer - * @param result buffer size + * @param result buffer, malloced (don't forget to free it yourself) */ -WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *waith, char *buf, - size_t bufSize) { +WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *waith, char **buf) { WaitressFetchBufCbBuffer_t buffer; + WaitressReturn_t wRet; + + memset (&buffer, 0, sizeof (buffer)); - buffer.buf = buffer.pos = buf; - buffer.max = bufSize; waith->data = &buffer; waith->callback = WaitressFetchBufCb; - return WaitressFetchCall (waith); + wRet = WaitressFetchCall (waith); + *buf = buffer.buf; + return wRet; } /* write () wrapper with poll () timeout diff --git a/libwaitress/src/waitress.h b/libwaitress/src/waitress.h index 3bcf524..d8c379d 100644 --- a/libwaitress/src/waitress.h +++ b/libwaitress/src/waitress.h @@ -67,7 +67,7 @@ char WaitressSplitUrl (const char *, char *, size_t, char *, size_t, char *, inline char WaitressSetUrl (WaitressHandle_t *, const char *); inline void WaitressSetHPP (WaitressHandle_t *, const char *, const char *, const char *); -WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *, char *, size_t); +WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *, char **); WaitressReturn_t WaitressFetchCall (WaitressHandle_t *); #endif /* _WAITRESS_H */ |