1
0
Fork 0

- check buffer length

- cosmetics
This commit is contained in:
mfranz 2006-11-23 09:26:43 +00:00
parent baa16f4995
commit d9015f6923
2 changed files with 12 additions and 8 deletions

View file

@ -1,5 +1,6 @@
/* $Id$ */ /* $Id$ */
/* gcc -O2 -g -pedantic -Wall fgfsclient.c -o fgfsclient */ /* gcc -O2 -g -pedantic -Wall fgfsclient.c -o fgfsclient */
/* USAGE: ./fgfsclient [hostname [port]] */
/* Public Domain */ /* Public Domain */
#include <stdio.h> #include <stdio.h>
@ -35,7 +36,7 @@ int fgfswrite(int sock, char *msg, ...)
char buf[MAXMSG]; char buf[MAXMSG];
va_start(va, msg); va_start(va, msg);
vsprintf(buf, msg, va); vsnprintf(buf, MAXMSG - 2, msg, va);
va_end(va); va_end(va);
printf("SEND: \t<%s>\n", buf); printf("SEND: \t<%s>\n", buf);
strcat(buf, "\015\012"); strcat(buf, "\015\012");

View file

@ -1,5 +1,6 @@
// $Id$ // $Id$
// g++ -O2 -g -pedantic -Wall fgfsclient.cxx -o fgfsclient -lstdc++ // g++ -O2 -g -pedantic -Wall fgfsclient.cxx -o fgfsclient -lstdc++
// USAGE: ./fgfsclient [hostname [port]]
// Public Domain // Public Domain
#include <errno.h> #include <errno.h>
@ -16,12 +17,14 @@
#include <string.h> #include <string.h>
const int MAXLEN = 256; const char *HOST = "localhost";
const unsigned PORT = 5501;
const int BUFLEN = 256;
class FGFSSocket { class FGFSSocket {
public: public:
FGFSSocket(const char *name, const unsigned port); FGFSSocket(const char *name, unsigned port);
~FGFSSocket(); ~FGFSSocket();
int write(const char *msg, ...); int write(const char *msg, ...);
@ -35,11 +38,11 @@ private:
int _sock; int _sock;
bool _connected; bool _connected;
unsigned _timeout; unsigned _timeout;
char _buffer[MAXLEN]; char _buffer[BUFLEN];
}; };
FGFSSocket::FGFSSocket(const char *hostname = "localhost", const unsigned port = 5501) : FGFSSocket::FGFSSocket(const char *hostname = HOST, unsigned port = PORT) :
_sock(-1), _sock(-1),
_connected(false), _connected(false),
_timeout(1) _timeout(1)
@ -96,7 +99,7 @@ int FGFSSocket::write(const char *msg, ...)
{ {
va_list va; va_list va;
ssize_t len; ssize_t len;
char buf[MAXLEN]; char buf[BUFLEN];
fd_set fd; fd_set fd;
struct timeval tv; struct timeval tv;
@ -108,7 +111,7 @@ int FGFSSocket::write(const char *msg, ...)
throw("FGFSSocket::write/select: timeout exceeded"); throw("FGFSSocket::write/select: timeout exceeded");
va_start(va, msg); va_start(va, msg);
vsprintf(buf, msg, va); vsnprintf(buf, BUFLEN - 2, msg, va);
va_end(va); va_end(va);
std::cout << "SEND: " << buf << std::endl; std::cout << "SEND: " << buf << std::endl;
strcat(buf, "\015\012"); strcat(buf, "\015\012");
@ -138,7 +141,7 @@ const char *FGFSSocket::read(void)
throw("FGFSSocket::read/select: timeout exceeded"); throw("FGFSSocket::read/select: timeout exceeded");
} }
len = ::read(_sock, _buffer, MAXLEN - 1); len = ::read(_sock, _buffer, BUFLEN - 1);
if (len < 0) if (len < 0)
throw("FGFSSocket::read/read"); throw("FGFSSocket::read/read");
if (len == 0) if (len == 0)