sprintf : garbage appended

This is a discussion on sprintf : garbage appended within the C Programming forums, part of the General Programming Boards category; Hi, I'm performing a simple sprintf : Code: char var[50]; sprintf(var, "%s", var2); let's say var2 is set to "hello ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    sprintf : garbage appended

    Hi,

    I'm performing a simple sprintf :

    Code:
    char var[50];
    
    sprintf(var, "%s", var2);
    let's say var2 is set to "hello world" (I've verified this is correct before the sprintf). The result of this is that var = "hello world,".

    However, when I do this :

    Code:
    sprintf(var, "%s\n", var2);
    I get what I want : "hello world".

    Was I using sprintf improperly before?

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That doesn't seem right. Are you sure there isn't something that you can't see at the end of the string [typically a carriage return, '\r' can sneak into strings read from input files or console]? Check strlen() and compare that with what you expect.

    sprintf() should work just fine without a newline - I suspect something else is wrong.

    --
    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.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Give us the smallest, but complete, full program that demonstrates this error.

    This one works as expected.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char szBuffer[100];
    	char szHello[50] = "Hello, world!";
    	
    	sprintf(szBuffer, "&#37;s", szHello);
    	
    	printf("szBuffer = %s\nszHello = %s\n\n", szBuffer, szHello);
    	
    	return 0;
    }
    Output:

    Code:
    szBuffer = Hello, world!
    szHello = Hello, world!
    I suspect you may have a '\0' problem with var2.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    I'll have to get a full program, but in the meantime here's more code that may be affecting :

    Code:
    char var[50];
    char var2[50];
    
    cgiFormStringNoNewlines("variable", var2, 50)
    mysql_real_escape_string(&db, var, var, (unsigned long)strlen(var));
    sprintf(var, "&#37;s", var2);
    Yes, I'm using the cgic library. The thing that is interesting is that when var2 is set to a string that has a space in it,
    that is when I have the problem, not otherwise.

    Also, I tested it again, and var was appended with ":" this time.

    Finally, what did you mean by "I suspect you may have a '\0' problem with var2."?

    Thanks
    Last edited by yeller; 12-14-2007 at 11:46 AM.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You've probably omitted a NUL terminator in 'var2'. NUL terminators if you don't already know, represent the end of the string.

    You should narrow down the problem.
    Code:
    char var[50];
    char var2[50];
    
    cgiFormStringNoNewlines("variable", var2, sizeof(var2));
    mysql_real_escape_string(&db, var, var, (unsigned long)strlen(var));
    sprintf(var, "&#37;s", var2);
    It's either one of those functions,
    the first = http://csourcesearch.net/package/aut...Newlines/604,1

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by yeller View Post
    Finally, what did you mean by "I suspect you may have a '\0' problem with var2."?
    Since a string in C is basically a block of contiguous sequence of chars that are ended with a '\0', I meant that you may not have a '\0' at the intended place in var2, thereby making all functions continue processing past the end of the string until coming across a '\0' later on.

    If you have a block of memory like this:

    Code:
    'H', 'i', '!', '\n', '\0', 'h', 'y', '5', 'f', 's', '\0', 'x'
    And the string starts at the first char, then the string should be "Hi!\n". If, however, you lose the first '\0' -- let's say it got overwritten with another '\n', then you'll have this interpretted as the string: "Hi!\n\nhy5s". Obviously not what you meant.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    "Hi!\n\nhy5s"
    Actually, "Hi!\n\nhy5fs" -- but we know what you mean.
    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.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Stop making me look bad.

    Yes, you're right. lol...

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    I seem to have found the problem. I've had a similar issue before, and a similar fix before.

    The code I'm working with at one point uses the basename command. For some reason, using this on a string variable has devastating effects (as seen in this case).

    The fix : What I did was simply create a new variable, copied the original var's data to it, and then send that on to the basename command.

    While basename works correctly, it seems to hurt the variable that is passed to it.

    Thanks for all replies anyhow.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > The fix : What I did was simply create a new variable, copied the original var's data to it,
    > and then send that on to the basename command.
    Very few of the standard C library functions (eg. memmove) work reliably when the "source" and "destination" areas overlap.

    For example, never assume that this is going to produce the effect you want.
    sprintf( b, "%s", b );
    OK, most of the time this example will work, but something like this is almost sure to fail horribly.
    sprintf( b, "/var/%s", b );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 03:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 12:35 PM

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