(OT)

* Badra wrote on Fri, Apr 18, 2008 at 17:03 +0200:
> I have also unsigned char B that stores a binary value. I need to
> convert it to hexadecimal, I do:
> 
> char *table = "0123456789abcdef";
> int i;
> for (i=0; i<strlen(B); i++)
>      {
>       B[2*i+1] = table[B[i] & 0xf];
>       B[2*i] = table[(B[i] & 0xf0) >> 4];
>      }
> 
> The above two converts work correctly. 

Sure?

You have a `unsigned char B', that usually is a 8 bit value and
invoke strlen on it? Do you mean a `const unsigned char*'
pointing to binary data?

Still, it would convert excluding first 0x00 byte. But I think it
is worse than that.

------------------------------------------------------------------->8=======
      1 #include <stdio.h>
      2 #include <assert.h>
      3 
      4 int
      5 main(void)
      6 {
      7     unsigned int guard = 0xdeadbeef;
      8     const unsigned char B[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 
0x00, 0xff };
      9     char *table = "0123456789abcdef";
     10     int i;
     11     for (i=0; i<strlen(B); i++)
     12     {
     13         B[2*i+1] = table[B[i] & 0xf];
     14         B[2*i] = table[(B[i] & 0xf0) >> 4];
     15     }
     16     assert(guard == 0xdeadbeef);
     17     return 0;
     18 }
=======8<-------------------------------------------------------------------

# gcc -ggdb x2.c
x2.c: In function `main':
x2.c:13: warning: assignment of read-only location
x2.c:14: warning: assignment of read-only location

# gdb a.out
(gdb) b 12
Breakpoint 1 at 0x80483d7: file x2.c, line 12.
13              B[2*i+1] = table[B[i] & 0xf];
(gdb) p /x guard
$1 = 0xdeadbeef
(gdb) p /x [EMAIL PROTECTED](B)
$2 = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x0, 0xff}
(gdb) cond 1 (i>7)
(gdb) c
Continuing.

Breakpoint 1, main () at x2.c:13
13              B[2*i+1] = table[B[i] & 0xf];
(gdb)  p /x guard
$3 = 0x31333333
(gdb) p /x [EMAIL PROTECTED](B)
$4 = {0x30, 0x31, 0x33, 0x31, 0x33, 0x33, 0x33, 0x31}

    here, the stack already is overflowed. strlen(i) is something undefined
    because no EOS zero anywhere in B.
    (beside the fact that B contains wrong data, I think
     0x30, 0x31, 0x30, 0x32, 0x30, 0x33, .... was intended)

(gdb) d 1
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x400969b0 in strlen () from /lib/i686/libc.so.6
0x400969b0 in strlen () from /lib/i686/libc.so.6



(gdb) up
#1  0x080483cd in main () at x2.c:11
11          for (i=0; i<strlen(B); i++)
(gdb) p &B
$5 = (const unsigned char (*)[8]) 0x33333323






oki,

Steffen
 
About Ingenico Throughout the world businesses rely on Ingenico for secure and 
expedient electronic transaction acceptance. Ingenico products leverage proven 
technology, established standards and unparalleled ergonomics to provide 
optimal reliability, versatility and usability. This comprehensive range of 
products is complemented by a global array of services and partnerships, 
enabling businesses in a number of vertical sectors to accept transactions 
anywhere their business takes them.
www.ingenico.com This message may contain confidential and/or privileged 
information. If you are not the addressee or authorized to receive this for the 
addressee, you must not use, copy, disclose or take any action based on this 
message or any information herein. If you have received this message in error, 
please advise the sender immediately by reply e-mail and delete this message. 
Thank you for your cooperation.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to