From 02b924e147458b1c8f6fb8ed3ca2ede955ed37c9 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 6 Nov 2020 10:33:57 -0500 Subject: [PATCH] Instrument float bug --- ext/liquid_c/lexer.c | 6 ++++++ ext/liquid_c/liquid.c | 2 ++ ext/liquid_c/usage.c | 18 ++++++++++++++++++ ext/liquid_c/usage.h | 9 +++++++++ test/unit/block_test.rb | 15 +++++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 ext/liquid_c/usage.c create mode 100644 ext/liquid_c/usage.h diff --git a/ext/liquid_c/lexer.c b/ext/liquid_c/lexer.c index fa5d99c8..3fb351dd 100644 --- a/ext/liquid_c/lexer.c +++ b/ext/liquid_c/lexer.c @@ -1,5 +1,6 @@ #include "liquid.h" #include "lexer.h" +#include "usage.h" #include const char *symbol_names[TOKEN_END] = { @@ -107,6 +108,11 @@ const char *lex_one(const char *start, const char *end, lexer_token_t *token) } } + // Instrument for bug: https://github.com/Shopify/liquid-c/pull/120 + if (c == '-' && str + 1 < end && str[1] == '.') { + usage_increment("liquid_c_negative_float_without_integer"); + } + if (ISDIGIT(c) || c == '-') { int has_dot = 0; cur = str; diff --git a/ext/liquid_c/liquid.c b/ext/liquid_c/liquid.c index aebf3231..dc9a2eb8 100644 --- a/ext/liquid_c/liquid.c +++ b/ext/liquid_c/liquid.c @@ -10,6 +10,7 @@ #include "context.h" #include "variable_lookup.h" #include "vm.h" +#include "usage.h" ID id_evaluate; ID id_to_liquid; @@ -82,5 +83,6 @@ void Init_liquid_c(void) init_liquid_context(); init_liquid_variable_lookup(); init_liquid_vm(); + init_liquid_usage(); } diff --git a/ext/liquid_c/usage.c b/ext/liquid_c/usage.c new file mode 100644 index 00000000..c72f5b72 --- /dev/null +++ b/ext/liquid_c/usage.c @@ -0,0 +1,18 @@ +#include "usage.h" + +static VALUE cLiquidUsage; +static ID id_increment; + +void usage_increment(const char *name) +{ + VALUE name_str = rb_str_new_cstr(name); + rb_funcall(cLiquidUsage, id_increment, 1, name_str); +} + +void init_liquid_usage() +{ + cLiquidUsage = rb_const_get(mLiquid, rb_intern("Usage")); + rb_global_variable(&cLiquidUsage); + + id_increment = rb_intern("increment"); +} diff --git a/ext/liquid_c/usage.h b/ext/liquid_c/usage.h new file mode 100644 index 00000000..43e5041b --- /dev/null +++ b/ext/liquid_c/usage.h @@ -0,0 +1,9 @@ +#ifndef LIQUID_USAGE_H +#define LIQUID_USAGE_H + +#include "liquid.h" + +void init_liquid_usage(); +void usage_increment(const char *name); + +#endif diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb index 0b267e87..64381f5b 100644 --- a/test/unit/block_test.rb +++ b/test/unit/block_test.rb @@ -31,6 +31,21 @@ def test_op_write_raw_w assert_equal(source, template.render!) end + # Test for bug: https://github.com/Shopify/liquid-c/pull/120 + def test_bug_120_instrument + calls = [] + Liquid::Usage.stub(:increment, ->(name) { calls << name }) do + Liquid::Template.parse("{{ -.1 }}") + end + assert_equal ["liquid_c_negative_float_without_integer"], calls + + calls = [] + Liquid::Usage.stub(:increment, ->(name) { calls << name }) do + Liquid::Template.parse("{{ .1 }}") + end + assert_equal [], calls + end + def test_disassemble_raw_w source = "a" * 2**8 template = Liquid::Template.parse(source)