diff --git a/NEWS.md b/NEWS.md index 8926803..5be7b14 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # glue 1.4.2 +* Unterminated comments in glue expression now throw an error (#227, @gaborcsardi) * Unterminated quotes in glue expressions now throw an error (#226, @gaborcsardi) * `glue_safe()` gives a slightly nicer error message * The required version of R is now 3.2 (#189) diff --git a/src/glue.c b/src/glue.c index 2682565..41ed2a0 100644 --- a/src/glue.c +++ b/src/glue.c @@ -182,6 +182,9 @@ SEXP glue_(SEXP x, SEXP f, SEXP open_arg, SEXP close_arg) { } else if (state == double_quote) { free(str); Rf_error("Unterminated quote (\")"); + } else if (state == comment) { + free(str); + Rf_error("Unterminated comment"); } free(str); diff --git a/tests/testthat/test-glue.R b/tests/testthat/test-glue.R index f4507e1..0eb0178 100644 --- a/tests/testthat/test-glue.R +++ b/tests/testthat/test-glue.R @@ -429,3 +429,10 @@ test_that("unterminated quotes are error", { expect_error(glue("{this doesn\"t work}"), "Unterminated quote") expect_error(glue("{this doesn't work}"), "Unterminated quote") }) + +test_that("unterminated comment", { + expect_error(glue("pre {1 + 5 # comment} post"), "Unterminated comment") + expect_error(glue("pre {1 + 5 # comment"), "Unterminated comment") + + expect_equal(glue("pre {1 + 5 + #comment\n 4} post"), "pre 10 post") +})