Format %ld is not suitable for use with size_t length.  (Same in various Log() messages added below.)
Should use %zu for (unsigned) size_t, %zd only for ssize_t and %ld only for a signed long parameter.