if (!ctx) {
                newptr = ber_memalloc_x( size, NULL );
                if ( newptr ) return newptr;
+               Debug(LDAP_DEBUG_ANY, "slap_sl_malloc of %lu bytes failed\n",
+                       (unsigned long) size, 0, 0);
                assert( 0 );
                exit( EXIT_FAILURE );
        }
 
        if (sh->sh_stack) {
                if ((char *)sh->sh_last + size >= (char *)sh->sh_end) {
+                       size -= 2*sizeof(ber_len_t);
                        Debug(LDAP_DEBUG_TRACE,
                                "slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
-                               (long)size, 0, 0);
+                               (unsigned long) size, 0, 0);
                        return ch_malloc(size);
                }
                newptr = sh->sh_last;
                        order++;
                } while (size_shift >>= 1);
 
+               size -= sizeof(ber_len_t);
+
                for (i = order; i <= sh->sh_maxorder &&
                                LDAP_LIST_EMPTY(&sh->sh_free[i-order_start]); i++);
 
                        diff = (unsigned long)((char*)ptr -
                                        (char*)sh->sh_base) >> (order + 1);
                        sh->sh_map[order-order_start][diff>>3] |= (1 << (diff & 0x7));
-                       *ptr++ = size - sizeof(ber_len_t);
+                       *ptr++ = size;
                        LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_new, so_link);
                        return((void*)ptr);
                } else if (i <= sh->sh_maxorder) {
                                                        (char*)sh->sh_base) >> (order+1);
                                        sh->sh_map[order-order_start][diff>>3] |=
                                                        (1 << (diff & 0x7));
-                                       *ptr++ = size - sizeof(ber_len_t);
+                                       *ptr++ = size;
                                        LDAP_LIST_INSERT_HEAD(
                                                        &sh->sh_free[j-1-order_start], so_right, so_link);
                                        LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_left, so_link);
                                                        &sh->sh_free[j-1-order_start], so_left, so_link);
                                }
                        }
-               } else {
-                       Debug( LDAP_DEBUG_TRACE,
-                               "slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
-                               (long)size, 0, 0);
-                       return (void*)ch_malloc(size);
                }
+               /* FIXME: missing return; guessing we failed... */
        }
 
-       /* FIXME: missing return; guessing... */
-       return NULL;
+       Debug(LDAP_DEBUG_TRACE,
+               "slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
+               (unsigned long) size, 0, 0);
+       return ch_malloc(size);
 }
 
 void *
 
        /* Not our memory? */
        if (!sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
-               /* duplicate of realloc behavior, oh well */
+               /* Like ch_realloc(), except not trying a new context */
                newptr = ber_memrealloc_x(ptr, size, NULL);
                if (newptr) {
                        return newptr;
                }
                Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
-                               (long) size, 0, 0);
+                       (unsigned long) size, 0, 0);
                assert(0);
                exit( EXIT_FAILURE );
        }
                Debug(level, "free list:\n", 0, 0, 0);
                so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
                while (so) {
-                       Debug(level, "%lx\n", (unsigned long) so->so_ptr, 0, 0);
+                       Debug(level, "%p\n", so->so_ptr, 0, 0);
                        so = LDAP_LIST_NEXT(so, so_link);
                }
        }