Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: error in hh length modifier when used with sscanf

Reply
Thread Tools

Re: error in hh length modifier when used with sscanf

 
 
CBFalconer
Guest
Posts: n/a
 
      11-09-2003
Bill Bennett wrote:
> "CBFalconer" <> wrote in message
> > Bill Bennett wrote:
> > >
> > > unsigned char buffer[] = "$$$$$$$$";
> > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > > sscanf("55", "%hhx", buffer+2);
> > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > >
> > > I expect
> > > 24 24 24 24 24 24 24 24
> > > 24 24 55 24 24 24 24 24
> > > but I get
> > > 24 24 24 24 24 24 24 24
> > > 24 24 55 0 0 0 24 24
> > >
> > > In other words, I expected one byte to be stored but instead 4
> > > are stored, trampling adjacent bytes.
> > >
> > > Using %hx instead of %hhx has the correct result, only 2 bytes
> > > are stored.
> > >
> > > Seems like an error in how the hh is handled, right?

> >
> > WRONG. You got what you asked for. Look up the meaning of hh in
> > C99 (it is new since C90). You are lucky you didn't get some sort
> > of bus alignment violation, so it would appear you are using
> > something x86 based, with sizeof(int) == 4 and sizeof(short) == 2
> > that stores integers low byte first.

>
> In the "standard C library" documentation provided with lcc, there is no
> separate description of the format spec for input vs output, so you have to
> guess a bit. But it says "hh means unsigned char or signed char" which would
> lead me to believe that when used in scanf only one byte is stored (assuming
> a char is one byte on the machine you're compiling to).
>
> In the ISO/IEC 9899 C spec it says specifically for fscanf:
>
> hh Specifies that a following d , i , o , u , x , X , or n conversion
> specifier applies to an argument with type pointer to signed char or
> unsigned char.


There appears to be a conflict here between N869 and the summaries
on <http://www.dinkumware.com>. This may be an error in N869 that
has been revised in the actual standard. The proper place to
thrash this out is on comp.lang.c and comp.std.c. followups and
cross-posts set.

Please do not send direct mail when discussions belong in the
newsgroup. Please do not top-post (fixed).

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
 
 
 
Douglas A. Gwyn
Guest
Posts: n/a
 
      11-09-2003
Only one byte should have been modified by that sscanf.

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      11-10-2003
CBFalconer <> wrote in message news:<>...
> Bill Bennett wrote:
> > "CBFalconer" <> wrote in message
> > > Bill Bennett wrote:
> > > >
> > > > unsigned char buffer[] = "$$$$$$$$";
> > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > > > sscanf("55", "%hhx", buffer+2);
> > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > > >
> > > > I expect
> > > > 24 24 24 24 24 24 24 24
> > > > 24 24 55 24 24 24 24 24
> > > > but I get
> > > > 24 24 24 24 24 24 24 24
> > > > 24 24 55 0 0 0 24 24
> > > >
> > > > In other words, I expected one byte to be stored but instead 4
> > > > are stored, trampling adjacent bytes.
> > > >
> > > > Using %hx instead of %hhx has the correct result, only 2 bytes
> > > > are stored.
> > > >
> > > > Seems like an error in how the hh is handled, right?
> > >
> > > WRONG. You got what you asked for. Look up the meaning of hh in
> > > C99 (it is new since C90). You are lucky you didn't get some sort
> > > of bus alignment violation, so it would appear you are using
> > > something x86 based, with sizeof(int) == 4 and sizeof(short) == 2
> > > that stores integers low byte first.

....
> > In the ISO/IEC 9899 C spec it says specifically for fscanf:
> >
> > hh Specifies that a following d , i , o , u , x , X , or n conversion
> > specifier applies to an argument with type pointer to signed char or
> > unsigned char.

>
> There appears to be a conflict here between N869 and the summaries
> on <http://www.dinkumware.com>. This may be an error in N869 that
> has been revised in the actual standard. The proper place to
> thrash this out is on comp.lang.c and comp.std.c. followups and
> cross-posts set.


As far as I can see, both n869.pdf and the final standard say the same
thing, that "%hhx" is for reading in a single unsigned char in
hexadecimal format. How are you reading them to allow four bytes to
get trashed?
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      11-10-2003
In < > (James Kuyper) writes:

>CBFalconer <> wrote in message news:<>...
>> Bill Bennett wrote:
>> > "CBFalconer" <> wrote in message
>> > > Bill Bennett wrote:
>> > > >
>> > > > unsigned char buffer[] = "$$$$$$$$";
>> > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
>> > > > sscanf("55", "%hhx", buffer+2);
>> > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
>> > > >
>> > > > I expect
>> > > > 24 24 24 24 24 24 24 24
>> > > > 24 24 55 24 24 24 24 24
>> > > > but I get
>> > > > 24 24 24 24 24 24 24 24
>> > > > 24 24 55 0 0 0 24 24
>> > > >
>> > > > In other words, I expected one byte to be stored but instead 4
>> > > > are stored, trampling adjacent bytes.
>> > > >
>> > > > Using %hx instead of %hhx has the correct result, only 2 bytes
>> > > > are stored.
>> > > >
>> > > > Seems like an error in how the hh is handled, right?
>> > >
>> > > WRONG. You got what you asked for. Look up the meaning of hh in
>> > > C99 (it is new since C90). You are lucky you didn't get some sort
>> > > of bus alignment violation, so it would appear you are using
>> > > something x86 based, with sizeof(int) == 4 and sizeof(short) == 2
>> > > that stores integers low byte first.

>...
>> > In the ISO/IEC 9899 C spec it says specifically for fscanf:
>> >
>> > hh Specifies that a following d , i , o , u , x , X , or n conversion
>> > specifier applies to an argument with type pointer to signed char or
>> > unsigned char.

>>
>> There appears to be a conflict here between N869 and the summaries
>> on <http://www.dinkumware.com>. This may be an error in N869 that
>> has been revised in the actual standard. The proper place to
>> thrash this out is on comp.lang.c and comp.std.c. followups and
>> cross-posts set.

>
>As far as I can see, both n869.pdf and the final standard say the same
>thing, that "%hhx" is for reading in a single unsigned char in
>hexadecimal format. How are you reading them to allow four bytes to
>get trashed?


The undefined behaviour in the printf calls can explain *anything*,
doesn't it? %x expects unsigned int, while buffer[i] most likely gets
promoted to signed int.

There would have been a lot less traps in the C language if *all* the
shorter unsigned types got promoted to unsigned int. Preserving both
value and signedness makes a lot more sense than preserving value only.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
Bill Bennett
Guest
Posts: n/a
 
      11-10-2003
"James Kuyper" <> wrote in message
news: om...
> CBFalconer <> wrote in message

news:<>...
> > Bill Bennett wrote:
> > > "CBFalconer" <> wrote in message
> > > > Bill Bennett wrote:
> > > > >
> > > > > unsigned char buffer[] = "$$$$$$$$";
> > > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > > > > sscanf("55", "%hhx", buffer+2);
> > > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > > > >
> > > > > I expect
> > > > > 24 24 24 24 24 24 24 24
> > > > > 24 24 55 24 24 24 24 24
> > > > > but I get
> > > > > 24 24 24 24 24 24 24 24
> > > > > 24 24 55 0 0 0 24 24
> > > > >
> > > > > In other words, I expected one byte to be stored but instead 4
> > > > > are stored, trampling adjacent bytes.
> > > > >
> > > > > Using %hx instead of %hhx has the correct result, only 2 bytes
> > > > > are stored.
> > > > >
> > > > > Seems like an error in how the hh is handled, right?
> > > >
> > > > WRONG. You got what you asked for. Look up the meaning of hh in
> > > > C99 (it is new since C90). You are lucky you didn't get some sort
> > > > of bus alignment violation, so it would appear you are using
> > > > something x86 based, with sizeof(int) == 4 and sizeof(short) == 2
> > > > that stores integers low byte first.

> ...
> > > In the ISO/IEC 9899 C spec it says specifically for fscanf:
> > >
> > > hh Specifies that a following d , i , o , u , x , X , or n

conversion
> > > specifier applies to an argument with type pointer to signed char or
> > > unsigned char.

> >
> > There appears to be a conflict here between N869 and the summaries
> > on <http://www.dinkumware.com>. This may be an error in N869 that
> > has been revised in the actual standard. The proper place to
> > thrash this out is on comp.lang.c and comp.std.c. followups and
> > cross-posts set.

>
> As far as I can see, both n869.pdf and the final standard say the same
> thing, that "%hhx" is for reading in a single unsigned char in
> hexadecimal format. How are you reading them to allow four bytes to
> get trashed?


Re: How are you reading them to allow four bytes to get trashed?

The exact example that shows the problem is near the top of this thread, and
of this post.


 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-10-2003
Bill Bennett wrote:
>
> "James Kuyper" <> wrote in message
> news: om...
> > CBFalconer <> wrote in message

> news:<>...
> > > Bill Bennett wrote:
> > > > "CBFalconer" <> wrote in message
> > > > > Bill Bennett wrote:
> > > > > >
> > > > > > unsigned char buffer[] = "$$$$$$$$";
> > > > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > > > > > sscanf("55", "%hhx", buffer+2);
> > > > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > > > > >
> > > > > > I expect
> > > > > > 24 24 24 24 24 24 24 24
> > > > > > 24 24 55 24 24 24 24 24
> > > > > > but I get
> > > > > > 24 24 24 24 24 24 24 24
> > > > > > 24 24 55 0 0 0 24 24
> > > > > >
> > > > > > In other words, I expected one byte to be stored but instead 4
> > > > > > are stored, trampling adjacent bytes.
> > > > > >
> > > > > > Using %hx instead of %hhx has the correct result, only 2 bytes
> > > > > > are stored.
> > > > > >
> > > > > > Seems like an error in how the hh is handled, right?
> > > > >
> > > > > WRONG. You got what you asked for. Look up the meaning of hh in
> > > > > C99 (it is new since C90). You are lucky you didn't get some sort
> > > > > of bus alignment violation, so it would appear you are using
> > > > > something x86 based, with sizeof(int) == 4 and sizeof(short) == 2
> > > > > that stores integers low byte first.

> > ...
> > > > In the ISO/IEC 9899 C spec it says specifically for fscanf:
> > > >
> > > > hh Specifies that a following d , i , o , u , x , X , or n

> conversion
> > > > specifier applies to an argument with type pointer to signed char or
> > > > unsigned char.
> > >
> > > There appears to be a conflict here between N869 and the summaries
> > > on <http://www.dinkumware.com>. This may be an error in N869 that
> > > has been revised in the actual standard. The proper place to
> > > thrash this out is on comp.lang.c and comp.std.c. followups and
> > > cross-posts set.

> >
> > As far as I can see, both n869.pdf and the final standard say the same
> > thing, that "%hhx" is for reading in a single unsigned char in
> > hexadecimal format. How are you reading them to allow four bytes to
> > get trashed?

>
> Re: How are you reading them to allow four bytes to get trashed?
>
> The exact example that shows the problem is near the top of this thread, and
> of this post.


Yes, I've seen that example of legal code that is producing results that
seem to be inconsistent with the requirements for C99. I've also read
responses which implied that there is some way to read n896 so that it
allows such behavior. I'm asking for an explanation of that way of
reading it. As far as I can see, the quoted text from n869 matches the
text from C99 that mandates different behavior. Of course, there's the
problem that, technically, it uses the wrong format code for the
printf()'s, but I doubt that correcting that error would change the
puzzelling results.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-10-2003
James Kuyper wrote:
> Bill Bennett wrote:
> > "James Kuyper" <> wrote in message
> > > CBFalconer <> wrote in message
> > > > Bill Bennett wrote:
> > > > > "CBFalconer" <> wrote in message
> > > > > > Bill Bennett wrote:
> > > > > > >
> > > > > > > unsigned char buffer[] = "$$$$$$$$";
> > > > > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > > > > > > sscanf("55", "%hhx", buffer+2);
> > > > > > > for (int i=0; i<8; i++) printf("%6x ", buffer[i]); printf("\n");
> > > > > > >
> > > > > > > I expect
> > > > > > > 24 24 24 24 24 24 24 24
> > > > > > > 24 24 55 24 24 24 24 24
> > > > > > > but I get
> > > > > > > 24 24 24 24 24 24 24 24
> > > > > > > 24 24 55 0 0 0 24 24
> > > > > > >
> > > > > > > In other words, I expected one byte to be stored but instead 4
> > > > > > > are stored, trampling adjacent bytes.
> > > > > > >
> > > > > > > Using %hx instead of %hhx has the correct result, only 2 bytes
> > > > > > > are stored.
> > > > > > >
> > > > > > > Seems like an error in how the hh is handled, right?
> > > > > >
> > > > > > WRONG. You got what you asked for. Look up the meaning of hh in
> > > > > > C99 (it is new since C90). You are lucky you didn't get some sort
> > > > > > of bus alignment violation, so it would appear you are using
> > > > > > something x86 based, with sizeof(int) == 4 and sizeof(short) == 2
> > > > > > that stores integers low byte first.
> > > ...
> > > > > In the ISO/IEC 9899 C spec it says specifically for fscanf:
> > > > >
> > > > > hh Specifies that a following d , i , o , u , x , X , or n
> > > > > conversion specifier applies to an argument with type pointer
> > > > > to signed char or unsigned char.
> > > >
> > > > There appears to be a conflict here between N869 and the summaries
> > > > on <http://www.dinkumware.com>. This may be an error in N869 that
> > > > has been revised in the actual standard. The proper place to
> > > > thrash this out is on comp.lang.c and comp.std.c. followups and
> > > > cross-posts set.
> > >
> > > As far as I can see, both n869.pdf and the final standard say the
> > > same thing, that "%hhx" is for reading in a single unsigned char in
> > > hexadecimal format. How are you reading them to allow four bytes to
> > > get trashed?

> >
> > Re: How are you reading them to allow four bytes to get trashed?
> >
> > The exact example that shows the problem is near the top of this
> > thread, and of this post.

>
> Yes, I've seen that example of legal code that is producing results
> that seem to be inconsistent with the requirements for C99. I've also
> read responses which implied that there is some way to read n896 so
> that it allows such behavior. I'm asking for an explanation of that
> way of reading it. As far as I can see, the quoted text from n869
> matches the text from C99 that mandates different behavior. Of
> course, there's the problem that, technically, it uses the wrong
> format code for the printf()'s, but I doubt that correcting that
> error would change the puzzelling results.


I have deliberately not snipped anything from this discussion. I
agree than the standard and N869 seem to dictate a read into a
single byte with the hhx specifier. I would hope to see someone
from dinkumware weigh in here, as my earliest reply was based on
their summary of the C99 meaning.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf length modifier Spoon C Programming 12 04-27-2007 04:51 AM
Server Error in '/' Application, Length cannot be less than zero. Parameter name: length Yugioh017@yahoo.com ASP .Net Security 0 02-11-2007 06:31 AM
Cannot access with modifier error. wooks Java 1 01-20-2007 09:49 AM
How to create a format string used by eg sscanf devrobcom@hotmail.com C Programming 3 06-08-2006 02:11 AM
Compile error: invalid type modifier within pointer declarator Per Johansson C++ 3 11-07-2004 09:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57