NO

Author Topic: sprintf  (Read 7147 times)

cane

  • Guest
sprintf
« on: October 19, 2005, 08:01:18 PM »
WTF is this?! Every kind of shit is coming out! :evil:

Code: [Select]

#include <stdio.h>
#include <stdlib.h>

typedef struct {
   char a1[7];
} foo;

foo x;

char y[7];

int main(int argc, char *argv[])
{
    sprintf(x.a1,"%.6X",rand());
    sprintf(y,"%.6X",rand());

    printf("%s\n", x.a1);
    printf("%s\n", y);

    return 0;
}


The big problem is with the char arry in the struct.
sprintf works "better" with char array y except for an extra char being appended (?!).

JohnF

  • Guest
sprintf
« Reply #1 on: October 19, 2005, 09:37:59 PM »
typedef struct {
   char a1[7];
} foo;

foo x;

char y[7];

===========
sprintf Precision
The precision specifies the minimum number of characters for type d, i, o, u, x or X
===========

What happens if 8 chars are sent to the arrays?

John

cane

  • Guest
sprintf
« Reply #2 on: October 19, 2005, 10:07:45 PM »
nothing comes out...

JohnF

  • Guest
sprintf
« Reply #3 on: October 20, 2005, 01:03:25 AM »
Increase the char arrays to 10

======================
typedef struct {
   char a1[10];
} foo;

foo x;

char y[10];
======================

and see what happens.

John

cane

  • Guest
sprintf
« Reply #4 on: October 20, 2005, 02:30:37 AM »
Ok, I've got it. As you said, "The precision specifies the minimum number of characters for type d, i, o, u, x or X"... I was a tricked by the fact x.a1 and y were lying one next to the other in memory and being the width of the arrays not enough to hold the strings, sprintf continued beyond the bouds. So strange stuff was printed until a null terminator was found.

Ok, let's update the bugs chart: -1 PellesC, +1 my brain...  #-o

BTW, isn't there a way to directly truncate/zero-pad the hex number with precision specifiers? That is, to specify how many digits to print AT MOST.

Thanks for the help, bye.

Offline jack

  • Member
  • *
  • Posts: 62
sprintf
« Reply #5 on: October 20, 2005, 05:01:53 AM »
Code: [Select]

sprintf(y,"%06X",rand());

JohnF

  • Guest
sprintf
« Reply #6 on: October 20, 2005, 07:34:18 AM »
Quote from: "cane"
Ok, I've got it. As you said, "The precision specifies the minimum number of characters for type d, i, o, u, x or X"... I was a tricked by the fact x.a1 and y were lying one next to the other in memory and being the width of the arrays not enough to hold the strings, sprintf continued beyond the bouds. So strange stuff was printed until a null terminator was found.


A typical buffer overrun, a common outcome would be a crash.
 
Quote

BTW, isn't there a way to directly truncate/zero-pad the hex number with precision specifiers? That is, to specify how many digits to print AT MOST.


Not that I know of because the number would become basically meaningless. However there are other things you can do.

sprintf(x.a1, "%.06X", rand() & 0xffffff);

or after the sprintf

x.a1[6] = '\0';

or

snprintf(x.a1, 7, "%.06X", rand());
x.a1[6] = '\0';

John