Thread: Buffer overflow? Won't join channel sometimes -- IRC Bot in C

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    11

    Buffer overflow? Won't join channel sometimes -- IRC Bot in C

    http://uranther.pastebin.com/510905

    The code is above. It will connect to that server but it won't join the channel. With the same code it does connect and join the channel. What am I missing? irc.neverside.com is UnrealIRCd if you're wondering.

    I was wondering if the weird characters (SĘ(p) I get from the message printed from the server are a buffer overflow somewhere but I can't find where. Is that attributing to the flakiness of the connecting etc? Or is there something fundamentally wrong with my code?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    n = read(sockfd, buffer, BUFFER - 1);
    printf("%s\n", buffer);
    You never null terminate the buffer before you print it out. Do this:
    Code:
    n = read(sockfd, buffer, BUFFER - 1);
    buffer[n] = 0;
    printf("%s\n", buffer);

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Also, you are calling strlen() way too many times. Save the string lengths into an integer so you don't have to keep calculating the length of the same string over and over again.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Oh, and you will run into problems with the following code:
    Code:
    if (strstr(buffer, "PING :"))
    What happens if a user sends a chat message with "PING :" somewhere in it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  2. Whats wrong with my IRC Bot??
    By peradox in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-23-2005, 05:16 PM
  3. help with IRC channel ban
    By MisterSako in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-05-2004, 01:23 PM
  4. cprogramming.com IRC channel
    By codec in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 05-06-2004, 10:31 AM