Thread: Help with Pointers / Strings

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Unhappy Help with Pointers / Strings

    I have been trying to get a program that someone else wrote to work on my Ubuntu computer. The following string was defined as

    char msg[256];

    Then the following function was built

    Code:
    void
    sprt_hex(char *line, unsigned char *frame, time_t *sys_time, time_t *clock_time)
    {
      char hex_str[3];
      int i, len;
      
      len = datalen[(int) frame[0]];
     // printf("%ld\n", *sys_time);     //debug line
     // printf("%ld\n", *clock_time);     //debug line
      sprintf(line, "%ld %ld", *sys_time, *clock_time);
    //  printf("%s\n",line);
      for (i = 0; i < len; i++)
      {
        sprintf(hex_str," %x", frame[i]);
    //	printf("%x ", frame[i]);
        strcat(line, hex_str);
      }
      strcat(line,"\n");
      //debug
     // printf("\n");
     printf("Line %s", line);[/B]
    //  printf("%02d\n",strlen(line));
      //debug end
    }

    This function was called with....with debugging statements printf...

    Code:
     sprt_hex(msg, frame,  &sys_time, &clock_time);
     printf("MSG after %s\n", msg);
     printf("string length %03d\n", strlen(msg));
    Program output is ...
    Line 1193795307 1193792367 3 21 37 0 82 1
    MSG after �f
    string length 004

    The function call to sprt_hex should send the pointer of msg to the function. The char *line is built properly. The value of line should populate that pointer location for msg, but instead I am passing jibberish.

    Help please!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Sounds like you're corrupting your buffer somehow. Make sure you're not writing into any buffer more than it has been defined to hold.

    With so many calls to sprintf() and strcat() without any checks, it's possible a disaster will strike with those conditions.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sprintf(hex_str," &#37;x", frame[i]);
    A 3 char array for this is rarely enough as well.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Need more guidance

    MacGyver,

    Forgive me, I am by no stretch of the imagination an experience programmer so I may need additional guidance on the alternatives to the sprintf commands.

    The way I read the output, the sprintf seemed to work properly in populating "line". It seems that "line" does not properly point back to "msg" as it should. I am not sure how the buffer could be changing between the last printf and the end of the function.

    Also, I use the printf command to check the sting length to ensure I have not over filled the buffer. It is also interesting to not that the string length of "line" and "msg" are different.

    Salem,

    It seems that this part of the program is working since "line" is being properly populated. I just do not understand why "line" does not equal "msg".

    Thanks to all for your time and help.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Salem was pointing out that hex_str only contains 3 characters. With this code:
    Code:
    sprintf(hex_str," &#37;x", frame[i]);
    hex_str[0] is a space, and you need to leave space for the NULL -- which means that frame[i] can only take up one hex digit. Can you guarantee this? Probably not, so make the buffer larger.

    As for why the strings are not equal -- perhaps because you have a buffer overrun, as MacGyver suggested.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Smile Thanks so much for the help.

    I changed hex_str[3] to hex_str[5] and everything seems to be working. The "line" variable matches the "msg" as it should. I guess now I do not understand why "line" ever looked correct if the frame[i] did not fit in hex_str.

    Also, I would still be happy to improve the code with any checks for sprintf() that you recommend. Any suggestions?

    Thanks again to all.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One thing you can use is the fact that sprintf() reteurns the number of characters it wrote, so
    Code:
    char s[10];
    if (sprintf(s, "%s", somestring) > 9) ... error ...
    is one thing you can do. Note that it returns the number before the end-marker, so a 9 character string returns 9. Note that the above check is just checking that the string did fit after the fact - if it doesn't fit, it will overwrite whatever is after the string given to sprintf.

    Making sure that the string is PLENTY long enough is another option.

    Or if your environment supports "snprintf", that would solve the problem of writing past the end of the sitring. You'd still get "bad" output, but you won't overwrite some other variable, only not get the full string you expected:
    Code:
    char s[10];
    snprintf(s, sizeof(s), "%s", somestring);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Pointers With Strings
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 02-14-2006, 06:28 AM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. pointers to strings
    By LiLgirL in forum C Programming
    Replies: 3
    Last Post: 04-23-2004, 02:38 PM
  5. More on pointers and strings
    By mart_man00 in forum C Programming
    Replies: 4
    Last Post: 02-10-2003, 06:10 PM