From e956bcf5d6533de9bbf2bbefae60c32e52348cf9 Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Thu, 31 Mar 2022 00:58:40 +0100 Subject: fs: fix off-by-one in fs.dirname() function Make sure fs.dirname() doesn't truncate the last character of the returned path. Previously ucv_string_new_length was called with a length which no longer included the last character (which had just been tested not to be a '/' or '.' and hence broke the loop at that point). Signed-off-by: Daniel Golle [testcase added] Signed-off-by: Paul Spooren [testcase folded into this commit and fixed] Signed-off-by: Jo-Philipp Wich --- lib/fs.c | 2 +- tests/custom/04_bugs/34_dirname_off_by_one | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/custom/04_bugs/34_dirname_off_by_one diff --git a/lib/fs.c b/lib/fs.c index bae5d28..99f6072 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -948,7 +948,7 @@ uc_fs_dirname(uc_vm_t *vm, size_t nargs) if (i == 0) return ucv_string_new("/"); - return ucv_string_new_length(s, i); + return ucv_string_new_length(s, i + 1); } static uc_value_t * diff --git a/tests/custom/04_bugs/34_dirname_off_by_one b/tests/custom/04_bugs/34_dirname_off_by_one new file mode 100644 index 0000000..34ef7c7 --- /dev/null +++ b/tests/custom/04_bugs/34_dirname_off_by_one @@ -0,0 +1,16 @@ +Make sure fs.dirname() doesn't truncate the last character of the +returned path. Previously ucv_string_new_length was called with a +length which no longer included the last character (which had just +been tested not to be a '/' or '.' and hence broke the loop at that +point). + +-- Testcase -- +{% + fs = require('fs'); + printf("%s\n", fs.dirname('/etc/config/wireless')); +%} +-- End -- + +-- Expect stdout -- +/etc/config +-- End -- -- cgit v1.2.3