diff options
author | Max Kellermann <max@duempel.org> | 2008-09-18 01:05:14 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2008-09-18 01:05:14 +0200 |
commit | bf8da42199fe351c844338ac6c09d715b25d34ed (patch) | |
tree | 3d2b8ebab852207fdc6a44ec52cf22579a403b89 | |
parent | 7fff0ce33c1ebf8806827b6c5fb7dfb978ff5cc3 (diff) | |
download | mpd-bf8da42199fe351c844338ac6c09d715b25d34ed.tar.gz mpd-bf8da42199fe351c844338ac6c09d715b25d34ed.tar.xz mpd-bf8da42199fe351c844338ac6c09d715b25d34ed.zip |
libmpdclient: use memmove() instead of strcpy() for moving the buffer
In general, don't treat the input buffer as a null-terminated string:
don't append '\0', don't use strchr() and strtok(). To delete
consumed portions of the buffer, strcpy() is bad anyway, because it
does not allow overlapping buffers.
-rw-r--r-- | src/libmpdclient.c | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/libmpdclient.c b/src/libmpdclient.c index e63c20bbf..8d1670f96 100644 --- a/src/libmpdclient.c +++ b/src/libmpdclient.c @@ -386,7 +386,7 @@ mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) { mpd_Connection * connection = malloc(sizeof(mpd_Connection)); struct timeval tv; fd_set fds; - strcpy(connection->buffer,""); + connection->buflen = 0; connection->bufstart = 0; strcpy(connection->errorStr,""); @@ -410,7 +410,7 @@ mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) { if (err < 0) return connection; - while(!(rt = strstr(connection->buffer,"\n"))) { + while(!(rt = memchr(connection->buffer, '\n', connection->buflen))) { tv.tv_sec = connection->timeout.tv_sec; tv.tv_usec = connection->timeout.tv_usec; FD_ZERO(&fds); @@ -429,7 +429,6 @@ mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) { return connection; } connection->buflen+=readed; - connection->buffer[connection->buflen] = '\0'; } else if(err<0) { if (SELECT_ERRNO_IGNORE) @@ -454,8 +453,8 @@ mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) { if (mpd_parseWelcome(connection, host, port, connection->buffer) == 0) connection->doneProcessing = 1; - strcpy(connection->buffer,rt+1); - connection->buflen = strlen(connection->buffer); + connection->buflen -= rt + 1 - connection->buffer; + memmove(connection->buffer, rt + 1, connection->buflen); return connection; } @@ -553,11 +552,13 @@ static void mpd_getNextReturnElement(mpd_Connection * connection) { bufferCheck = connection->buffer+connection->bufstart; while (connection->bufstart >= connection->buflen || - !(rt = strchr(bufferCheck, '\n'))) { + !(rt = memchr(bufferCheck, '\n', + connection->buffer + connection->buflen - + bufferCheck))) { if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) { memmove(connection->buffer, connection->buffer + connection->bufstart, - connection->buflen - connection->bufstart + 1); + connection->buflen - connection->bufstart); connection->buflen -= connection->bufstart; connection->bufstart = 0; } @@ -590,7 +591,6 @@ static void mpd_getNextReturnElement(mpd_Connection * connection) { return; } connection->buflen+=readed; - connection->buffer[connection->buflen] = '\0'; } else if(err<0 && SELECT_ERRNO_IGNORE) continue; else { |