fix miscellaneous compiler warnings

The attached patch tweaks things to avoid miscellaneous compiler warnings, from GCC and from the Clang static analyzer:

  • Avoid signed/unsigned comparison with judicious casts, justified by asserts.
  • Use the same type for indices into an array as are used for the size of the array.
  • Make sure variables are initialized even for error branches.
  • Avoid possible null pointer dereference in case of test failure.