Browse Source

Loops work better when they terminate. Non-terminating loops are easier
to diagnose when they don't trash the stack.


svn:r1460

Nick Mathewson 21 years ago
parent
commit
7451de5d97
1 changed files with 5 additions and 1 deletions
  1. 5 1
      src/common/util.c

+ 5 - 1
src/common/util.c

@@ -113,7 +113,8 @@ void hex_encode(const char *from, int fromlen, char *to)
 {
   const unsigned char *fp = from;
   static const char TABLE[] = "0123456789abcdef";
-  while (fromlen) {
+  assert(from && fromlen>=0 && to);
+  while (fromlen--) {
     *to++ = TABLE[*fp >> 4];
     *to++ = TABLE[*fp & 7];
     ++fp;
@@ -219,11 +220,13 @@ void *smartlist_choose(smartlist_t *sl) {
 
 void *smartlist_get(smartlist_t *sl, int idx)
 {
+  assert(sl && idx>=0 && idx < sl->num_used);
   return sl->list[idx];
 }
 void *smartlist_set(smartlist_t *sl, int idx, void *val)
 {
   void *old;
+  assert(sl && idx>=0 && idx < sl->num_used);
   old = sl->list[idx];
   sl->list[idx] = val;
   return old;
@@ -231,6 +234,7 @@ void *smartlist_set(smartlist_t *sl, int idx, void *val)
 void *smartlist_del(smartlist_t *sl, int idx)
 {
   void *old;
+  assert(sl && idx>=0 && idx < sl->num_used);
   old = sl->list[idx];
   sl->list[idx] = sl->list[--sl->num_used];
   return old;