Strange concat bugg

This is a discussion on Strange concat bugg within the C Programming forums, part of the General Programming Boards category; Hello! I'm writing my own shell, but I'm getting a strange error. I read a number of paths from a ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    40

    Strange concat bugg

    Hello!

    I'm writing my own shell, but I'm getting a strange error. I read a number of paths from a file when I start the shell, but for some bizarr reason I realised that any threechar commands in /usr/bin are impossible to run. All other commands work fine.

    So I tried some things and figured out there is a strange bug in my concat function which I can't figure out. I add two strings together, and it works fine, but whenever the total size of the string is 12 chars it becomes 15 chars. Below is the concat-function along with a main for testing it. The output when running this program is:

    Size 1: 9
    Size 2: 3
    Sum: 12
    The parts: /usr/bin/ - zip
    The concat string: /usr/bin/zip|
    Sum 15: /usr/bin/zip

    The program (concat test program that is..):

    Code:
    char* concat(char* strang1, char* strang2)
    {
      int x=0;
      int y=0;
      int cx=0;
      char* newStr;
      int size1;
      int size2;
      int sum;
    
      while(strang1[x]!=0)
        x++;
      size1=x;
      x=0;
      while(strang2[x]!=0)
        x++;
      size2=x;
      x=0;
      sum=size1+size2;
    
      printf("Size 1: %d\nSize 2: %d\nSum: %d\n",size1,size2,sum);
    
      //  newStr=(char*)malloc(sum*sizeof(char));
      printf("The parts: %s - %s\n",strang1,strang2);
      newStr=(char*)calloc(sum,sizeof(char));
    
      for (x=0;x<size1;x++)
        newStr[x]=strang1[x];
    
      for (cx=0;cx<size2;cx++)
        newStr[x+cx]=strang2[cx];
    
      printf("The concat string: %s|\n",newStr);
      return(newStr);
    }
    
    int main()
    {
      char* part1="/usr/bin/";
      char* part2="zip";
      char* total=concat(part1,part2);
      int x=0;
      while(total[x]!=0)
        x++;
    
      printf("Sum %d: %s\n",x,total);
      free(total);
      exit(0);
    }
    Does anyone have any idea why this happens? I can't figure it out.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    40

    Comment

    Just a comment about my question. Since someone might ask, I wanted to add that it works for both longer and short strings ("e.g. "/usr/bin/id" works, aswell as "/usr/bin/time".

    The problem occurs for both malloc and calloc, but only for this length.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    calloc sum + 1 so newStr is will be null-terminated.

    I hope you're including stdlib.h and stdio.h as well.

    gg

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Ok, thanks for the tip. I thought it was automatic, but I guess not.

    Yes, I'm including stdlib.h and stdio.h, I just didn't write it out since it didn't feel relevant :-)

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Thank you very much. Adding 1 to the sum actually made it work.

    I'm still curious why it worked for all other commands and all other string lengths though.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Zarniwoop View Post
    I'm still curious why it worked for all other commands and all other string lengths though.
    Pure luck. (or lack thereof, since it was hiding a bug).

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Seems like it, but still. It always worked for all other commands, and never worked for three-char commands in /usr/bin. It doesn't seem random, it was always the same.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    It doesn't seem random
    It just means you have same garbage on the heap when you start your buggy program. Go to another compiler - you get another results...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  2. "new" is strange
    By The Wazaa in forum C++ Programming
    Replies: 26
    Last Post: 03-06-2006, 11:32 AM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 01:56 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21