* [PATCH v1 1/1] xlog: fix out of static memory on metadata load @ 2018-08-15 12:32 Kirill Shcherbatov 2018-08-17 10:14 ` Vladimir Davydov 0 siblings, 1 reply; 6+ messages in thread From: Kirill Shcherbatov @ 2018-08-15 12:32 UTC (permalink / raw) To: tarantool-patches; +Cc: vdavydov.dev, Kirill Shcherbatov This problem triggered asan checks on start tarantool with existent xlog. We don't have to touch even static non-initialized memory. --- Branch: http://github.com/tarantool/tarantool/tree/kshch/vinyl-xlog-out-of-static-memory src/box/xlog.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/box/xlog.c b/src/box/xlog.c index 5ed11fc..797f153 100644 --- a/src/box/xlog.c +++ b/src/box/xlog.c @@ -261,8 +261,11 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, assert(val <= val_end); pos = eol + 1; - if (memcmp(key, INSTANCE_UUID_KEY, key_end - key) == 0 || - memcmp(key, INSTANCE_UUID_KEY_V12, key_end - key) == 0) { + size_t len = key_end - key; + if ((len == strlen(INSTANCE_UUID_KEY) && + memcmp(key, INSTANCE_UUID_KEY, len) == 0) || + (len == strlen(INSTANCE_UUID_KEY_V12) && + memcmp(key, INSTANCE_UUID_KEY_V12, key_end - key) == 0)) { /* * Instance: <uuid> */ @@ -277,19 +280,22 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, diag_set(XlogError, "can't parse instance UUID"); return -1; } - } else if (memcmp(key, VCLOCK_KEY, key_end - key) == 0){ + } else if (len == strlen(VCLOCK_KEY) && + memcmp(key, VCLOCK_KEY, len) == 0) { /* * VClock: <vclock> */ if (parse_vclock(val, val_end, &meta->vclock) != 0) return -1; - } else if (memcmp(key, PREV_VCLOCK_KEY, key_end - key) == 0) { + } else if (len == strlen(PREV_VCLOCK_KEY) && + memcmp(key, PREV_VCLOCK_KEY, len) == 0) { /* * PrevVClock: <vclock> */ if (parse_vclock(val, val_end, &meta->prev_vclock) != 0) return -1; - } else if (memcmp(key, VERSION_KEY, key_end - key) == 0) { + } else if (len == strlen(VERSION_KEY) && + memcmp(key, VERSION_KEY, len) == 0) { /* Ignore Version: for now */ } else { /* -- 2.7.4 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v1 1/1] xlog: fix out of static memory on metadata load 2018-08-15 12:32 [PATCH v1 1/1] xlog: fix out of static memory on metadata load Kirill Shcherbatov @ 2018-08-17 10:14 ` Vladimir Davydov 2018-08-17 12:39 ` [tarantool-patches] " Kirill Shcherbatov 0 siblings, 1 reply; 6+ messages in thread From: Vladimir Davydov @ 2018-08-17 10:14 UTC (permalink / raw) To: Kirill Shcherbatov; +Cc: tarantool-patches On Wed, Aug 15, 2018 at 03:32:02PM +0300, Kirill Shcherbatov wrote: > This problem triggered asan checks on start tarantool > with existent xlog. We don't have to touch even static > non-initialized memory. > --- > Branch: http://github.com/tarantool/tarantool/tree/kshch/vinyl-xlog-out-of-static-memory > > src/box/xlog.c | 16 +++++++++++----- > 1 file changed, 11 insertions(+), 5 deletions(-) > > diff --git a/src/box/xlog.c b/src/box/xlog.c > index 5ed11fc..797f153 100644 > --- a/src/box/xlog.c > +++ b/src/box/xlog.c > @@ -261,8 +261,11 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, > assert(val <= val_end); > pos = eol + 1; > > - if (memcmp(key, INSTANCE_UUID_KEY, key_end - key) == 0 || > - memcmp(key, INSTANCE_UUID_KEY_V12, key_end - key) == 0) { > + size_t len = key_end - key; > + if ((len == strlen(INSTANCE_UUID_KEY) && > + memcmp(key, INSTANCE_UUID_KEY, len) == 0) || > + (len == strlen(INSTANCE_UUID_KEY_V12) && > + memcmp(key, INSTANCE_UUID_KEY_V12, key_end - key) == 0)) { Too much of code duplication. Please add a helper for key matching. > /* > * Instance: <uuid> > */ > @@ -277,19 +280,22 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, > diag_set(XlogError, "can't parse instance UUID"); > return -1; > } > - } else if (memcmp(key, VCLOCK_KEY, key_end - key) == 0){ > + } else if (len == strlen(VCLOCK_KEY) && > + memcmp(key, VCLOCK_KEY, len) == 0) { > /* > * VClock: <vclock> > */ > if (parse_vclock(val, val_end, &meta->vclock) != 0) > return -1; > - } else if (memcmp(key, PREV_VCLOCK_KEY, key_end - key) == 0) { > + } else if (len == strlen(PREV_VCLOCK_KEY) && > + memcmp(key, PREV_VCLOCK_KEY, len) == 0) { > /* > * PrevVClock: <vclock> > */ > if (parse_vclock(val, val_end, &meta->prev_vclock) != 0) > return -1; > - } else if (memcmp(key, VERSION_KEY, key_end - key) == 0) { > + } else if (len == strlen(VERSION_KEY) && > + memcmp(key, VERSION_KEY, len) == 0) { > /* Ignore Version: for now */ > } else { > /* ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [tarantool-patches] Re: [PATCH v1 1/1] xlog: fix out of static memory on metadata load 2018-08-17 10:14 ` Vladimir Davydov @ 2018-08-17 12:39 ` Kirill Shcherbatov 2018-08-17 12:46 ` Vladimir Davydov 0 siblings, 1 reply; 6+ messages in thread From: Kirill Shcherbatov @ 2018-08-17 12:39 UTC (permalink / raw) To: tarantool-patches, Vladimir Davydov > Too much of code duplication. Please add a helper for key matching. Hi! ok. =========================================== src/box/xlog.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/box/xlog.c b/src/box/xlog.c index 5ed11fc..c870d9d 100644 --- a/src/box/xlog.c +++ b/src/box/xlog.c @@ -183,6 +183,13 @@ parse_vclock(const char *val, const char *val_end, struct vclock *vclock) return 0; } +static inline bool +xlog_meta_key_cmp(const char *key, uint32_t key_len, const char *str, + uint32_t str_len) +{ + return key_len != str_len || memcmp(key, str, key_len); +} + /** * Parse xlog meta from buffer, update buffer read * position in case of success @@ -261,8 +268,11 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, assert(val <= val_end); pos = eol + 1; - if (memcmp(key, INSTANCE_UUID_KEY, key_end - key) == 0 || - memcmp(key, INSTANCE_UUID_KEY_V12, key_end - key) == 0) { + size_t len = key_end - key; + if (xlog_meta_key_cmp(key, len, INSTANCE_UUID_KEY, + strlen(INSTANCE_UUID_KEY)) == 0 || + xlog_meta_key_cmp(key, len, INSTANCE_UUID_KEY_V12, + strlen(INSTANCE_UUID_KEY_V12)) == 0) { /* * Instance: <uuid> */ @@ -277,19 +287,22 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, diag_set(XlogError, "can't parse instance UUID"); return -1; } - } else if (memcmp(key, VCLOCK_KEY, key_end - key) == 0){ + } else if (xlog_meta_key_cmp(key, len, VCLOCK_KEY, + strlen(VCLOCK_KEY)) == 0) { /* * VClock: <vclock> */ if (parse_vclock(val, val_end, &meta->vclock) != 0) return -1; - } else if (memcmp(key, PREV_VCLOCK_KEY, key_end - key) == 0) { + } else if (xlog_meta_key_cmp(key, len, PREV_VCLOCK_KEY, + strlen(PREV_VCLOCK_KEY)) == 0) { /* * PrevVClock: <vclock> */ if (parse_vclock(val, val_end, &meta->prev_vclock) != 0) return -1; - } else if (memcmp(key, VERSION_KEY, key_end - key) == 0) { + } else if (xlog_meta_key_cmp(key, len, VERSION_KEY, + strlen(VERSION_KEY)) == 0) { /* Ignore Version: for now */ } else { /* -- 2.7.4 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [tarantool-patches] Re: [PATCH v1 1/1] xlog: fix out of static memory on metadata load 2018-08-17 12:39 ` [tarantool-patches] " Kirill Shcherbatov @ 2018-08-17 12:46 ` Vladimir Davydov 2018-08-17 14:46 ` Kirill Shcherbatov 0 siblings, 1 reply; 6+ messages in thread From: Vladimir Davydov @ 2018-08-17 12:46 UTC (permalink / raw) To: Kirill Shcherbatov; +Cc: tarantool-patches On Fri, Aug 17, 2018 at 03:39:50PM +0300, Kirill Shcherbatov wrote: > > Too much of code duplication. Please add a helper for key matching. > Hi! ok. > > =========================================== > > src/box/xlog.c | 23 ++++++++++++++++++----- > 1 file changed, 18 insertions(+), 5 deletions(-) > > diff --git a/src/box/xlog.c b/src/box/xlog.c > index 5ed11fc..c870d9d 100644 > --- a/src/box/xlog.c > +++ b/src/box/xlog.c > @@ -183,6 +183,13 @@ parse_vclock(const char *val, const char *val_end, struct vclock *vclock) > return 0; > } > > +static inline bool > +xlog_meta_key_cmp(const char *key, uint32_t key_len, const char *str, > + uint32_t str_len) What's the point of passing str_len when you can compute it right in this function with strlen? Also, please pass key_end instead of key_len to make function calls more compact. Also, if the function returns bool (which is OK), you shouldn't check its return value against 0. And you'd better call it xlog_meta_key_match or xlog_meta_key_equal and make it return true on match and false otherwise. Please fix. > +{ > + return key_len != str_len || memcmp(key, str, key_len); > +} > + > /** > * Parse xlog meta from buffer, update buffer read > * position in case of success > @@ -261,8 +268,11 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, > assert(val <= val_end); > pos = eol + 1; > > - if (memcmp(key, INSTANCE_UUID_KEY, key_end - key) == 0 || > - memcmp(key, INSTANCE_UUID_KEY_V12, key_end - key) == 0) { > + size_t len = key_end - key; > + if (xlog_meta_key_cmp(key, len, INSTANCE_UUID_KEY, > + strlen(INSTANCE_UUID_KEY)) == 0 || > + xlog_meta_key_cmp(key, len, INSTANCE_UUID_KEY_V12, > + strlen(INSTANCE_UUID_KEY_V12)) == 0) { > /* > * Instance: <uuid> > */ > @@ -277,19 +287,22 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, > diag_set(XlogError, "can't parse instance UUID"); > return -1; > } > - } else if (memcmp(key, VCLOCK_KEY, key_end - key) == 0){ > + } else if (xlog_meta_key_cmp(key, len, VCLOCK_KEY, > + strlen(VCLOCK_KEY)) == 0) { > /* > * VClock: <vclock> > */ > if (parse_vclock(val, val_end, &meta->vclock) != 0) > return -1; > - } else if (memcmp(key, PREV_VCLOCK_KEY, key_end - key) == 0) { > + } else if (xlog_meta_key_cmp(key, len, PREV_VCLOCK_KEY, > + strlen(PREV_VCLOCK_KEY)) == 0) { > /* > * PrevVClock: <vclock> > */ > if (parse_vclock(val, val_end, &meta->prev_vclock) != 0) > return -1; > - } else if (memcmp(key, VERSION_KEY, key_end - key) == 0) { > + } else if (xlog_meta_key_cmp(key, len, VERSION_KEY, > + strlen(VERSION_KEY)) == 0) { > /* Ignore Version: for now */ > } else { > /* ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [tarantool-patches] Re: [PATCH v1 1/1] xlog: fix out of static memory on metadata load 2018-08-17 12:46 ` Vladimir Davydov @ 2018-08-17 14:46 ` Kirill Shcherbatov 2018-08-17 15:20 ` Vladimir Davydov 0 siblings, 1 reply; 6+ messages in thread From: Kirill Shcherbatov @ 2018-08-17 14:46 UTC (permalink / raw) To: tarantool-patches, Vladimir Davydov > What's the point of passing str_len when you can compute it right in > this function with strlen? > > Also, please pass key_end instead of key_len to make function calls more > compact. > > Also, if the function returns bool (which is OK), you shouldn't check > its return value against 0. And you'd better call it xlog_meta_key_match > or xlog_meta_key_equal and make it return true on match and false > otherwise. > > Please fix. diff --git a/src/box/xlog.c b/src/box/xlog.c index 5ed11fc..ed6c06a 100644 --- a/src/box/xlog.c +++ b/src/box/xlog.c @@ -183,6 +183,12 @@ parse_vclock(const char *val, const char *val_end, struct vclock *vclock) return 0; } +static inline bool +xlog_meta_key_equal(const char *key, uint32_t key_len, const char *str) +{ + return key_len == strlen(str) && memcmp(key, str, key_len) == 0; +} + /** * Parse xlog meta from buffer, update buffer read * position in case of success @@ -261,8 +267,9 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, assert(val <= val_end); pos = eol + 1; - if (memcmp(key, INSTANCE_UUID_KEY, key_end - key) == 0 || - memcmp(key, INSTANCE_UUID_KEY_V12, key_end - key) == 0) { + uint32_t key_len = key_end - key; + if (xlog_meta_key_equal(key, key_len, INSTANCE_UUID_KEY) || + xlog_meta_key_equal(key, key_len, INSTANCE_UUID_KEY_V12)) { /* * Instance: <uuid> */ @@ -277,19 +284,19 @@ xlog_meta_parse(struct xlog_meta *meta, const char **data, diag_set(XlogError, "can't parse instance UUID"); return -1; } - } else if (memcmp(key, VCLOCK_KEY, key_end - key) == 0){ + } else if (xlog_meta_key_equal(key, key_len, VCLOCK_KEY)) { /* * VClock: <vclock> */ if (parse_vclock(val, val_end, &meta->vclock) != 0) return -1; - } else if (memcmp(key, PREV_VCLOCK_KEY, key_end - key) == 0) { + } else if (xlog_meta_key_equal(key, key_len, PREV_VCLOCK_KEY)) { /* * PrevVClock: <vclock> */ if (parse_vclock(val, val_end, &meta->prev_vclock) != 0) return -1; - } else if (memcmp(key, VERSION_KEY, key_end - key) == 0) { + } else if (xlog_meta_key_equal(key, key_len, VERSION_KEY)) { /* Ignore Version: for now */ } else { /* ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [tarantool-patches] Re: [PATCH v1 1/1] xlog: fix out of static memory on metadata load 2018-08-17 14:46 ` Kirill Shcherbatov @ 2018-08-17 15:20 ` Vladimir Davydov 0 siblings, 0 replies; 6+ messages in thread From: Vladimir Davydov @ 2018-08-17 15:20 UTC (permalink / raw) To: Kirill Shcherbatov; +Cc: tarantool-patches Pushed to 1.10 ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2018-08-17 15:20 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-08-15 12:32 [PATCH v1 1/1] xlog: fix out of static memory on metadata load Kirill Shcherbatov 2018-08-17 10:14 ` Vladimir Davydov 2018-08-17 12:39 ` [tarantool-patches] " Kirill Shcherbatov 2018-08-17 12:46 ` Vladimir Davydov 2018-08-17 14:46 ` Kirill Shcherbatov 2018-08-17 15:20 ` Vladimir Davydov
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox