From 9d6355413b6d33c574e851a16975907ec3aac4e4 Mon Sep 17 00:00:00 2001 From: Bin Fu <65430559+fubin1999@users.noreply.github.com> Date: Thu, 5 Nov 2026 01:53:06 +0900 Subject: [PATCH 1/4] fix: reject unclosed pGlyco branches --- NEWS.md | 2 ++ src/direct.cpp | 2 ++ tests/testthat/test-parse-pglyco.R | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/NEWS.md b/NEWS.md index ae26053..bad1744 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # glyparse (development version) +* `parse_pglyco_struc()` and `auto_parse()` reject structures with unclosed pGlyco branches instead of silently accepting truncated input. + # glyparse 1.0.1 * Fix a potential issue of the C++ code detected by memory sanitizers on M1 mac. diff --git a/src/direct.cpp b/src/direct.cpp index 5c1b903..4cd9ff2 100644 --- a/src/direct.cpp +++ b/src/direct.cpp @@ -32,6 +32,8 @@ static Record pglyco(const S &x) { stack.pop_back(); } } + if (!stack.empty()) + fail("Unbalanced pGlyco branch"); if (n != (int)ms.size() || n == 0 || (n == 1 && x.find(')') == S::npos)) fail("Invalid pGlyco structure"); return r; diff --git a/tests/testthat/test-parse-pglyco.R b/tests/testthat/test-parse-pglyco.R index 79b8e91..869dbbe 100644 --- a/tests/testthat/test-parse-pglyco.R +++ b/tests/testthat/test-parse-pglyco.R @@ -75,3 +75,16 @@ test_that("pGlyco array assembly preserves branched topology", { rep("??-?", 8) ) }) + +test_that("pGlyco rejects unclosed branches", { + invalid <- c("(N(H)", "(N(H", "(N(F)(N(H))") + expect_error(parse_pglyco_struc(invalid), "Can't parse") + expect_true(all(is.na(parse_pglyco_struc(invalid, on_failure = "na")))) + expect_true(all(is.na(auto_parse(invalid, on_failure = "na")))) + + input <- c(valid = "(N(H))", invalid = "(N(H)", missing = NA) + result <- parse_pglyco_struc(input, on_failure = "na") + expect_identical(names(result), names(input)) + expect_identical(unname(is.na(result)), c(FALSE, TRUE, TRUE)) + expect_equal(unname(result[1]), parse_pglyco_struc("(N(H))")) +}) From a48f373db4aec88ff750975aafeb7ed857855fda Mon Sep 17 00:00:00 2001 From: Bin Fu <65430559+fubin1999@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:00:10 +0900 Subject: [PATCH 2/4] fix: validate StrucGP depth markers before parsing --- NEWS.md | 2 ++ src/direct.cpp | 23 +++++++++++++++++++++++ tests/testthat/test-parse-strucgp.R | 15 +++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/NEWS.md b/NEWS.md index ae26053..8d62f7e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # glyparse (development version) +* `parse_strucgp_struc()` rejects unmatched depth markers and malformed content instead of silently turning truncated input into valid structures. + # glyparse 1.0.1 * Fix a potential issue of the C++ code detected by memory sanitizers on M1 mac. diff --git a/src/direct.cpp b/src/direct.cpp index 5c1b903..afd49dd 100644 --- a/src/direct.cpp +++ b/src/direct.cpp @@ -36,6 +36,28 @@ static Record pglyco(const S &x) { fail("Invalid pGlyco structure"); return r; } +static void validate_strucgp_markers(const S &x) { + std::vector stack; + for (size_t i = 0; i < x.size();) { + char c = x[i]; + if (c >= 'A' && c <= 'Z') { + if ((stack.empty() && i != 0) || i + 1 >= x.size() || + x[i + 1] < '1' || x[i + 1] > '5') + fail("Invalid StrucGP node"); + stack.push_back(c); + i += 2; + } else if (c >= 'a' && c <= 'z') { + if (stack.empty() || c != std::tolower(static_cast(stack.back()))) + fail("Unbalanced StrucGP depth marker"); + stack.pop_back(); + ++i; + } else { + fail("Invalid StrucGP depth marker"); + } + } + if (x.empty() || !stack.empty()) + fail("Unbalanced StrucGP depth marker"); +} static void strucgp_node(const S &x, Record &r, int parent, int depth = 0) { if (x.empty()) return; @@ -355,6 +377,7 @@ Record parse_direct(const S &x, const S &format, const Vocab &v) { if (format == "pglyco") return pglyco(x); if (format == "strucgp") { + validate_strucgp_markers(x); Record r; r.anomer = "??"; strucgp_node(x, r, 0); diff --git a/tests/testthat/test-parse-strucgp.R b/tests/testthat/test-parse-strucgp.R index 848cf35..92b2914 100644 --- a/tests/testthat/test-parse-strucgp.R +++ b/tests/testthat/test-parse-strucgp.R @@ -61,3 +61,18 @@ test_that("StrucGP array assembly preserves branched topology", { rep("??-?", 8) ) }) + +test_that("StrucGP rejects unmatched markers and skipped content", { + invalid <- c( + "A1B2bX", "A1B2b", "A1B2a", "A1B2baB3b", "A1B2bXB3ba" + ) + expect_error(parse_strucgp_struc(invalid), "Can't parse") + expect_true(all(is.na(parse_strucgp_struc(invalid, on_failure = "na")))) + expect_true(all(is.na(auto_parse(invalid, on_failure = "na")))) + + input <- c(valid = "A1B2bB3ba", invalid = "A1B2bXB3ba", missing = NA) + result <- parse_strucgp_struc(input, on_failure = "na") + expect_identical(names(result), names(input)) + expect_identical(unname(is.na(result)), c(FALSE, TRUE, TRUE)) + expect_equal(unname(result[1]), parse_strucgp_struc("A1B2bB3ba")) +}) From eb691327eaabab219813fba3ab1f8a1e48a11b86 Mon Sep 17 00:00:00 2001 From: fubin1999 <65430559+fubin1999@users.noreply.github.com> Date: Fri, 6 Nov 2026 13:15:21 +0800 Subject: [PATCH 3/4] test: require strictly closed glycan structures --- tests/testthat/test-native.R | 4 ---- tests/testthat/test-parse-pglyco.R | 7 +++++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-native.R b/tests/testthat/test-native.R index 43d5fa3..e04df1b 100644 --- a/tests/testthat/test-native.R +++ b/tests/testthat/test-native.R @@ -79,10 +79,6 @@ test_that("native ports preserve established tokenization and recovery", { )), c(NA_character_, "GalNAc(a1-") ) - expect_identical( - as.character(parse_pglyco_struc(c("(H(N)", "(H"), on_failure = "na")), - c("HexNAc(??-?)Hex(??-", NA_character_) - ) expect_identical( as.character(parse_strucgp_struc("A1B1a")), "Hex(??-?)Hex(??-" diff --git a/tests/testthat/test-parse-pglyco.R b/tests/testthat/test-parse-pglyco.R index 869dbbe..6d01f6a 100644 --- a/tests/testthat/test-parse-pglyco.R +++ b/tests/testthat/test-parse-pglyco.R @@ -77,7 +77,7 @@ test_that("pGlyco array assembly preserves branched topology", { }) test_that("pGlyco rejects unclosed branches", { - invalid <- c("(N(H)", "(N(H", "(N(F)(N(H))") + invalid <- c("(H(N)", "(H", "(N(H)", "(N(H", "(N(F)(N(H))") expect_error(parse_pglyco_struc(invalid), "Can't parse") expect_true(all(is.na(parse_pglyco_struc(invalid, on_failure = "na")))) expect_true(all(is.na(auto_parse(invalid, on_failure = "na")))) @@ -86,5 +86,8 @@ test_that("pGlyco rejects unclosed branches", { result <- parse_pglyco_struc(input, on_failure = "na") expect_identical(names(result), names(input)) expect_identical(unname(is.na(result)), c(FALSE, TRUE, TRUE)) - expect_equal(unname(result[1]), parse_pglyco_struc("(N(H))")) + expect_identical( + unname(as.character(result[1])), + as.character(parse_pglyco_struc("(N(H))")) + ) }) From 4da181ed19bf8c8ad31d7d8b4cdb775138315372 Mon Sep 17 00:00:00 2001 From: fubin1999 <65430559+fubin1999@users.noreply.github.com> Date: Fri, 6 Nov 2026 13:15:21 +0800 Subject: [PATCH 4/4] test: require strictly closed glycan structures --- tests/testthat/test-native.R | 4 ---- tests/testthat/test-parse-strucgp.R | 12 ++++++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-native.R b/tests/testthat/test-native.R index 43d5fa3..db5d205 100644 --- a/tests/testthat/test-native.R +++ b/tests/testthat/test-native.R @@ -83,10 +83,6 @@ test_that("native ports preserve established tokenization and recovery", { as.character(parse_pglyco_struc(c("(H(N)", "(H"), on_failure = "na")), c("HexNAc(??-?)Hex(??-", NA_character_) ) - expect_identical( - as.character(parse_strucgp_struc("A1B1a")), - "Hex(??-?)Hex(??-" - ) expect_identical( as.character(parse_linucs("[][b-D-Glcp]{[(4+1)][a-D-Glcp\n]{}}")), "Glc(a1-4)Glc(b1-" diff --git a/tests/testthat/test-parse-strucgp.R b/tests/testthat/test-parse-strucgp.R index 92b2914..ed8b366 100644 --- a/tests/testthat/test-parse-strucgp.R +++ b/tests/testthat/test-parse-strucgp.R @@ -64,7 +64,12 @@ test_that("StrucGP array assembly preserves branched topology", { test_that("StrucGP rejects unmatched markers and skipped content", { invalid <- c( - "A1B2bX", "A1B2b", "A1B2a", "A1B2baB3b", "A1B2bXB3ba" + "A1B1a", + "A1B2bX", + "A1B2b", + "A1B2a", + "A1B2baB3b", + "A1B2bXB3ba" ) expect_error(parse_strucgp_struc(invalid), "Can't parse") expect_true(all(is.na(parse_strucgp_struc(invalid, on_failure = "na")))) @@ -74,5 +79,8 @@ test_that("StrucGP rejects unmatched markers and skipped content", { result <- parse_strucgp_struc(input, on_failure = "na") expect_identical(names(result), names(input)) expect_identical(unname(is.na(result)), c(FALSE, TRUE, TRUE)) - expect_equal(unname(result[1]), parse_strucgp_struc("A1B2bB3ba")) + expect_identical( + unname(as.character(result[1])), + as.character(parse_strucgp_struc("A1B2bB3ba")) + ) })