[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

bug in hex_encode()



Hi,

there seems to be a nasty bug in hex_encode(). Doing e.g.

char in[] = { 0x40, 0x4f, 0 };
char out[128];
hex_encode(in, strlen(in), out);
puts(out);

outputs "4047" instead of "407f". Here's a patch to fix the problem:


--- tor-0.0.7.2/src/common/util.c.orig  Sat Aug  7 01:01:33 2004
+++ tor-0.0.7.2/src/common/util.c       Sat Aug  7 01:01:42 2004
@@ -233,21 +233,21 @@
  * write the result as a NUL-terminated string to <b>to</b>.  <b>to</b> must
  * have at least (2*fromlen)+1 bytes of free space.
  */
 void hex_encode(const char *from, int fromlen, char *to)
 {
   const unsigned char *fp = from;
   static const char TABLE[] = "0123456789abcdef";
   tor_assert(from && fromlen>=0 && to);
   while (fromlen--) {
     *to++ = TABLE[*fp >> 4];
-    *to++ = TABLE[*fp & 7];
+    *to++ = TABLE[*fp & 15];
     ++fp;
   }
   *to = '\0';
 }

 /** Return a pointer to a NUL-terminated hexidecimal string encoding
  * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
  * result does not need to be deallocated, but repeated calls to
  * hex_str will trash old results.
  */



best regards,
Timo Lindfors