From 5c5ef256b39665d1f882f12c76f54cbac6c807d0 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Fri, 31 Mar 2017 16:20:38 +0300 Subject: [PATCH] mdbx: change mdbx_dkey(). --- mdbx.h | 2 +- src/mdbx.c | 35 +++++++++++++++++++++-------------- test/test6.c | 5 +++-- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/mdbx.h b/mdbx.h index 3ebc59b3..301854e6 100644 --- a/mdbx.h +++ b/mdbx.h @@ -1625,7 +1625,7 @@ LIBMDBX_API int mdbx_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx); */ LIBMDBX_API int mdbx_reader_check(MDB_env *env, int *dead); -LIBMDBX_API char *mdbx_dkey(MDB_val *key, char *buf); +LIBMDBX_API char *mdbx_dkey(MDB_val *key, char *buf, const size_t bufsize); LIBMDBX_API int mdbx_env_close_ex(MDB_env *env, int dont_sync); diff --git a/src/mdbx.c b/src/mdbx.c index 2f4e17fb..8abf7666 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -332,7 +332,7 @@ txnid_t mdbx_debug_edge; * @ingroup debug * Invoke a function to display a key in hex. */ -#define DKEY(x) mdbx_dkey(x, kbuf) +#define DKEY(x) mdbx_dkey(x, kbuf, sizeof(kbuf)) /** An invalid page number. * Mainly used to denote an empty tree. @@ -821,7 +821,7 @@ static __inline pgno_t mdbx_dbg_pgno(MDB_page *mp) { * @param[in] buf the buffer to write into. Should always be #DKBUF. * @return The key in hexadecimal form. */ -char *mdbx_dkey(MDB_val *key, char *buf) { +char *mdbx_dkey(MDB_val *key, char *buf, const size_t bufsize) { #ifdef _MSC_VER (void)key; (void)buf; @@ -833,17 +833,24 @@ char *mdbx_dkey(MDB_val *key, char *buf) { if (!key) return ""; - if (key->mv_size > DKBUF_MAXKEYSIZE) - return "MDB_MAXKEYSIZE"; -/* may want to make this a dynamic check: if the key is mostly -* printable characters, print it as-is instead of converting to hex. */ -#if 1 - buf[0] = '\0'; - for (i = 0; i < key->mv_size; i++) - ptr += sprintf(ptr, "%02x", ((unsigned char *)key->mv_data)[i]); -#else - sprintf(buf, "%.*s", key->mv_size, key->mv_data); -#endif + const uint8_t *const data = key->mv_data; + bool is_ascii = true; + for (i = 0; is_ascii && i < key->mv_size; i++) + if (data[i] < ' ' || data[i] > 127) + is_ascii = false; + + if (is_ascii) + snprintf(buf, bufsize, "%.*s", + (key->mv_size > INT_MAX) ? INT_MAX : (int)key->mv_size, data); + else { + buf[0] = '\0'; + for (i = 0; i < key->mv_size; i++) { + int len = snprintf(ptr, bufsize - (ptr - buf), "%02x", data[i]); + if (len < 1) + break; + ptr += len; + } + } return buf; #endif /* _MSC_VER */ } @@ -6833,7 +6840,7 @@ static int mdbx_update_key(MDB_cursor *mc, MDB_val *key) { k2.mv_data = NODEKEY(node); k2.mv_size = node->mn_ksize; mdbx_debug("update key %u (ofs %u) [%s] to [%s] on page %zu", indx, ptr, - mdbx_dkey(&k2, kbuf2), DKEY(key), mp->mp_pgno); + mdbx_dkey(&k2, kbuf2, sizeof(kbuf2)), DKEY(key), mp->mp_pgno); } /* Sizes must be 2-byte aligned. */ diff --git a/test/test6.c b/test/test6.c index e9d6dd3c..03b6f7d1 100644 --- a/test/test6.c +++ b/test/test6.c @@ -113,8 +113,9 @@ int main(int argc, char *argv[]) { E(mdbx_cursor_get(cursor, &key, &data, MDB_FIRST)); do { - printf("key: %p %s, data: %p %.*s\n", key.mv_data, mdbx_dkey(&key, dkbuf), - data.mv_data, (int)data.mv_size, (char *)data.mv_data); + printf("key: %p %s, data: %p %.*s\n", key.mv_data, + mdbx_dkey(&key, dkbuf, sizeof(dkbuf)), data.mv_data, + (int)data.mv_size, (char *)data.mv_data); } while ((rc = mdbx_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0); CHECK(rc == MDB_NOTFOUND, "mdbx_cursor_get"); mdbx_cursor_close(cursor);