Code

libmpdclient: use memmove() instead of strcpy() for moving the buffer
authorMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 23:05:14 +0000 (01:05 +0200)
committerMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 23:05:14 +0000 (01:05 +0200)
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.

src/libmpdclient.c

index e63c20bbf673c5aa3eb2950bbc5ba4f4ce3ffe5a..8d1670f96951d79adc3fbde3a82e91450f90a345 100644 (file)
@@ -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 {