Thread: variable arguments and vsprintf

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    variable arguments and vsprintf

    I am trying to create a function that will accept multiple arguments in a string a la printf, and send them through a socket. I feel I'm on the right track, however... I'm wondering why this isn't working. It seems that when i use more than one special character, such as %s, it starts doing the others on newlines (I need it to send through the socket as one complete string)

    what am I doing wrong?


    Code:
    int sendraw(int socket, char *message, ...)
    {
    	char buffer[512];
    	va_list args;
    	va_start(args, message);
    	vsprintf(buffer, message, args);
    	va_end(args);
    	send(socket, buffer, strlen(buffer), 0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Debug it (print buffer to a file), and compare.

    Are you sure there isn't a buffer overflow?

    Your send assumes (falsely) that send() always sends the whole message in a single call. It doesn't (on a TCP connection).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed