From 96683ca4d3611e7c25364a1dbf326435727757d2 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 18 Sep 2008 01:05:16 +0200 Subject: [PATCH] libmpdclient: smaller error buffer 512 characters should be well enough for error messages which are actually being displayed somewhere. Use sizeof(errorStr) instead of MPD_BUFFER_MAX_LENGTH everywhere. Do proper length checking when copying the output buffer to errorStr. --- src/libmpdclient.c | 42 ++++++++++++++++++++++-------------------- src/libmpdclient.h | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/libmpdclient.c b/src/libmpdclient.c index 8d1670f..b29ffe7 100644 --- a/src/libmpdclient.c +++ b/src/libmpdclient.c @@ -87,7 +87,7 @@ static int winsock_dll_error(mpd_Connection *connection) if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0 || LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2 ) { - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "Could not find usable WinSock DLL."); connection->error = MPD_ERROR_SYSTEM; return 1; @@ -141,7 +141,7 @@ static int mpd_connect(mpd_Connection * connection, const char * host, int port, error = getaddrinfo(host, service, &hints, &addrinfo); if (error) { - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "host \"%s\" not found: %s",host, gai_strerror(error)); connection->error = MPD_ERROR_UNKHOST; return -1; @@ -151,7 +151,7 @@ static int mpd_connect(mpd_Connection * connection, const char * host, int port, /* create socket */ connection->sock = socket(res->ai_family, SOCK_STREAM, res->ai_protocol); if (connection->sock < 0) { - snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "problems creating socket: %s", strerror(errno)); connection->error = MPD_ERROR_SYSTEM; @@ -172,7 +172,7 @@ static int mpd_connect(mpd_Connection * connection, const char * host, int port, freeaddrinfo(addrinfo); if (connection->sock < 0) { - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "problems connecting to \"%s\" on port" " %i: %s",host,port, strerror(errno)); connection->error = MPD_ERROR_CONNPORT; @@ -192,7 +192,7 @@ static int mpd_connect(mpd_Connection * connection, const char * host, int port, struct sockaddr_in sin; if(!(he=gethostbyname(host))) { - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "host \"%s\" not found",host); connection->error = MPD_ERROR_UNKHOST; return -1; @@ -227,7 +227,7 @@ static int mpd_connect(mpd_Connection * connection, const char * host, int port, /* connect stuff */ if (do_connect_fail(connection, dest, destlen)) { - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "problems connecting to \"%s\" on port" " %i",host,port); connection->error = MPD_ERROR_CONNPORT; @@ -306,7 +306,7 @@ static int mpd_parseWelcome(mpd_Connection * connection, const char * host, int int i; if(strncmp(output,MPD_WELCOME_MESSAGE,strlen(MPD_WELCOME_MESSAGE))) { - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "mpd not running on port %i on host \"%s\"", port,host); connection->error = MPD_ERROR_NOTMPD; @@ -319,8 +319,7 @@ static int mpd_parseWelcome(mpd_Connection * connection, const char * host, int if(tmp) connection->version[i] = strtol(tmp,&test,10); if (!tmp || (test[0] != '.' && test[0] != '\0')) { - snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "error parsing version number at " "\"%s\"", &output[strlen(MPD_WELCOME_MESSAGE)]); @@ -369,7 +368,7 @@ static int mpd_connect_un(mpd_Connection * connection, close(connection->sock); connection->sock = 0; - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "problems connecting to \"%s\": %s", host, strerror(errno)); connection->error = MPD_ERROR_CONNPORT; @@ -389,8 +388,7 @@ mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) { connection->buflen = 0; connection->bufstart = 0; - strcpy(connection->errorStr,""); - connection->error = 0; + mpd_clearError(connection); connection->doneProcessing = 0; connection->commandList = 0; connection->listOks = 0; @@ -421,7 +419,7 @@ mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) { &(connection->buffer[connection->buflen]), MPD_BUFFER_MAX_LENGTH-connection->buflen,0); if(readed<=0) { - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "problems getting a response from" " \"%s\" on port %i : %s",host, port, strerror(errno)); @@ -434,14 +432,14 @@ mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) { if (SELECT_ERRNO_IGNORE) continue; snprintf(connection->errorStr, - MPD_BUFFER_MAX_LENGTH, + sizeof(connection->errorStr), "problems connecting to \"%s\" on port" " %i",host,port); connection->error = MPD_ERROR_CONNPORT; return connection; } else { - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "timeout in attempting to get a response from" " \"%s\" on port %i",host,port); connection->error = MPD_ERROR_NORESPONSE; @@ -500,7 +498,7 @@ static void mpd_executeCommand(mpd_Connection *connection, if(ret<=0) { if (SENDRECV_ERRNO_IGNORE) continue; - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "problems giving command \"%s\"",command); connection->error = MPD_ERROR_SENDING; return; @@ -515,7 +513,7 @@ static void mpd_executeCommand(mpd_Connection *connection, if(commandLen>0) { perror(""); - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "timeout sending command \"%s\"",command); connection->error = MPD_ERROR_TIMEOUT; return; @@ -631,11 +629,16 @@ static void mpd_getNextReturnElement(mpd_Connection * connection) { } if(strncmp(output,"ACK",strlen("ACK"))==0) { + size_t length = strlen(output); char * test; char * needle; int val; - strcpy(connection->errorStr, output); + if (length >= sizeof(connection->errorStr)) + length = sizeof(connection->errorStr) - 1; + + memcpy(connection->errorStr, output, length); + connection->errorStr[length] = 0; connection->error = MPD_ERROR_ACK; connection->errorCode = MPD_ACK_ERROR_UNK; connection->errorAt = MPD_ERROR_AT_UNK; @@ -664,9 +667,8 @@ static void mpd_getNextReturnElement(mpd_Connection * connection) { connection->returnElement = mpd_newReturnElement(name,&(value[1])); } else { - snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, + snprintf(connection->errorStr, sizeof(connection->errorStr), "error parsing: %s:%s",name,value); - connection->errorStr[MPD_BUFFER_MAX_LENGTH] = '\0'; connection->error = 1; } } diff --git a/src/libmpdclient.h b/src/libmpdclient.h index e91013b..c4a22b2 100644 --- a/src/libmpdclient.h +++ b/src/libmpdclient.h @@ -110,7 +110,7 @@ typedef struct _mpd_Connection { /* use this to check the version of mpd */ int version[3]; /* IMPORTANT, you want to get the error messages from here */ - char errorStr[MPD_BUFFER_MAX_LENGTH+1]; + char errorStr[512]; int errorCode; int errorAt; /* this will be set to MPD_ERROR_* if there is an error, 0 if not */ -- 2.30.2