From 44c8d02e51ff29c94d15855ea82c3d4814f5ec5c Mon Sep 17 00:00:00 2001 From: Sven Date: Thu, 9 Apr 2026 19:24:00 +0200 Subject: [PATCH 1/6] Call correct real function in the position0 hook --- src/fis_hook.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fis_hook.c b/src/fis_hook.c index 2b5c12a..9f6f3ea 100644 --- a/src/fis_hook.c +++ b/src/fis_hook.c @@ -168,7 +168,7 @@ jlong JNICALL fis_length0_hook(JNIEnv *env, jobject thiz) { jlong JNICALL fis_position0_hook(JNIEnv *env, jobject thiz) { if (!has_tag(thiz)) { - return fis_real_length0(env, thiz); + return fis_real_position0(env, thiz); } jclass class = (*env)->GetObjectClass(env, thiz); From 019c8e58bb7bba02659f346b5a848f4d2cf15f24 Mon Sep 17 00:00:00 2001 From: Sven Date: Thu, 9 Apr 2026 19:32:21 +0200 Subject: [PATCH 2/6] Fix possible out of bounds write --- src/utils.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/utils.c b/src/utils.c index d2724a8..185251c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -59,6 +59,16 @@ void parse_file(char **data_ptr, size_t fsize, const char *filename) { if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c == 95) { size_t len = strlen(env_var); + if (len + 1 >= 4096) { + fprintf(stderr, + "\033[0G\e[0;31m[SubstAgent] Failed to expand environment " + "variable \"%s\" in file %s on line %d because it is too " + "big\e[0m\n", + env_var, filename, line_count); + begin_env_index = -1; + env_var[0] = '\0'; + continue; + } env_var[len] = c; env_var[len + 1] = '\0'; } else if (c == '}') { From 24d5ba8d1212cd334d1682dfc008d73ef4652d2a Mon Sep 17 00:00:00 2001 From: Sven Date: Thu, 9 Apr 2026 19:32:35 +0200 Subject: [PATCH 3/6] Fix possible out of bounds read --- src/utils.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils.c b/src/utils.c index 185251c..8af5ad4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -47,7 +47,6 @@ char *substitute(const char *value, int prefix_char_matched_index, size_t i, void parse_file(char **data_ptr, size_t fsize, const char *filename) { char *data = *data_ptr; int line_count = 1; - ; int begin_env_index = -1; char env_var[4096]; env_var[0] = '\0'; @@ -123,7 +122,7 @@ void parse_file(char **data_ptr, size_t fsize, const char *filename) { } } else { if (c == '^') { - if (data[i + 1] == '{') { + if (i + 1 < fsize && data[i + 1] == '{') { begin_env_index = i; i++; } From 5eff59134711384e19d97d743e1cac686a26b61c Mon Sep 17 00:00:00 2001 From: Sven Date: Thu, 9 Apr 2026 19:40:44 +0200 Subject: [PATCH 4/6] Improve EOF check --- src/fis_hook.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fis_hook.c b/src/fis_hook.c index 9f6f3ea..016bd12 100644 --- a/src/fis_hook.c +++ b/src/fis_hook.c @@ -139,8 +139,8 @@ jint JNICALL fis_read0_hook(JNIEnv *env, jobject thiz) { } struct fis_file_data *fd = &file->value; - char read_byte = fd->data[fd->index++]; - if (fd->data[fd->index] == '\0') { + jint read_byte = (unsigned char)fd->data[fd->index++]; + if (fd->index >= fd->length) { free(fd->data); fd->data = NULL; } From 81bdf8b477e1e97df0678287509fc0e5eed1675b Mon Sep 17 00:00:00 2001 From: Sven Date: Thu, 9 Apr 2026 19:40:51 +0200 Subject: [PATCH 5/6] Fix tests --- tests/Test13.java | 6 ++++-- tests/data/test1.txt | 2 +- tests/data/test10.txt | 2 +- tests/data/test11.txt | 2 +- tests/data/test12.txt | 2 +- tests/data/test13.nacf | 2 +- tests/data/test2.txt | 2 +- tests/data/test3.txt | 2 +- tests/data/test4.txt | 2 +- tests/data/test5.txt | 2 +- tests/data/test6.txt | 2 +- tests/data/test7.txt | 2 +- tests/data/test8.txt | 2 +- tests/data/test9.txt | 2 +- 14 files changed, 17 insertions(+), 15 deletions(-) diff --git a/tests/Test13.java b/tests/Test13.java index 4fe9e71..e7c7586 100644 --- a/tests/Test13.java +++ b/tests/Test13.java @@ -1,10 +1,12 @@ + import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; // Test file that is not a config file class Test13 { - static String correctString = "This is not a config file so ${SAME_LENGTH_ENV} shouldn't be substituted"; + + static String correctString = "This is not a config file so ^{SAME_LENGTH_ENV} shouldn't be substituted"; public static String toHexDump(String s) { byte[] bytes = s.getBytes(java.nio.charset.StandardCharsets.UTF_8); @@ -38,4 +40,4 @@ class Test13 { throw e; } } -} \ No newline at end of file +} diff --git a/tests/data/test1.txt b/tests/data/test1.txt index ac896ba..5a70683 100644 --- a/tests/data/test1.txt +++ b/tests/data/test1.txt @@ -1 +1 @@ -Hello ${SAME_LENGTH_ENV} \ No newline at end of file +Hello ^{SAME_LENGTH_ENV} \ No newline at end of file diff --git a/tests/data/test10.txt b/tests/data/test10.txt index 41e2bea..9f83f57 100644 --- a/tests/data/test10.txt +++ b/tests/data/test10.txt @@ -1 +1 @@ -According to all known laws of aviation, there is no way a bee should be able to fly ${SAME_LENGTH_ENV} \ No newline at end of file +According to all known laws of aviation, there is no way a bee should be able to fly ^{SAME_LENGTH_ENV} \ No newline at end of file diff --git a/tests/data/test11.txt b/tests/data/test11.txt index 28d934b..0e7e2a4 100644 --- a/tests/data/test11.txt +++ b/tests/data/test11.txt @@ -1 +1 @@ -According to all known laws of aviation, there is no way a bee should be able to fly ${SHORTER_ENV} \ No newline at end of file +According to all known laws of aviation, there is no way a bee should be able to fly ^{SHORTER_ENV} \ No newline at end of file diff --git a/tests/data/test12.txt b/tests/data/test12.txt index ba48dff..fe09178 100644 --- a/tests/data/test12.txt +++ b/tests/data/test12.txt @@ -1 +1 @@ -${SHORTER_ENV} \ No newline at end of file +^{SHORTER_ENV} \ No newline at end of file diff --git a/tests/data/test13.nacf b/tests/data/test13.nacf index 75c2861..7cc5b0f 100644 --- a/tests/data/test13.nacf +++ b/tests/data/test13.nacf @@ -1 +1 @@ -This is not a config file so ${SAME_LENGTH_ENV} shouldn't be substituted \ No newline at end of file +This is not a config file so ^{SAME_LENGTH_ENV} shouldn't be substituted \ No newline at end of file diff --git a/tests/data/test2.txt b/tests/data/test2.txt index c771bec..bb67c8f 100644 --- a/tests/data/test2.txt +++ b/tests/data/test2.txt @@ -1 +1 @@ -Hello ${SHORTER_ENV} \ No newline at end of file +Hello ^{SHORTER_ENV} \ No newline at end of file diff --git a/tests/data/test3.txt b/tests/data/test3.txt index 16e6ce6..83a5268 100644 --- a/tests/data/test3.txt +++ b/tests/data/test3.txt @@ -1 +1 @@ -Hello ${LONGER_ENV} \ No newline at end of file +Hello ^{LONGER_ENV} \ No newline at end of file diff --git a/tests/data/test4.txt b/tests/data/test4.txt index 159c530..d8d50b5 100644 --- a/tests/data/test4.txt +++ b/tests/data/test4.txt @@ -1 +1 @@ -Hi ${SAME_LENGTH_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file +Hi ^{SAME_LENGTH_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file diff --git a/tests/data/test5.txt b/tests/data/test5.txt index ddbeee9..9f301eb 100644 --- a/tests/data/test5.txt +++ b/tests/data/test5.txt @@ -1 +1 @@ -Hi ${SHORTER_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file +Hi ^{SHORTER_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file diff --git a/tests/data/test6.txt b/tests/data/test6.txt index 51729d2..8480b64 100644 --- a/tests/data/test6.txt +++ b/tests/data/test6.txt @@ -1 +1 @@ -Hi ${LONGER_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file +Hi ^{LONGER_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file diff --git a/tests/data/test7.txt b/tests/data/test7.txt index 159c530..d8d50b5 100644 --- a/tests/data/test7.txt +++ b/tests/data/test7.txt @@ -1 +1 @@ -Hi ${SAME_LENGTH_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file +Hi ^{SAME_LENGTH_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file diff --git a/tests/data/test8.txt b/tests/data/test8.txt index ddbeee9..9f301eb 100644 --- a/tests/data/test8.txt +++ b/tests/data/test8.txt @@ -1 +1 @@ -Hi ${SHORTER_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file +Hi ^{SHORTER_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file diff --git a/tests/data/test9.txt b/tests/data/test9.txt index 51729d2..8480b64 100644 --- a/tests/data/test9.txt +++ b/tests/data/test9.txt @@ -1 +1 @@ -Hi ${LONGER_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file +Hi ^{LONGER_ENV} According to all known laws of aviation, there is no way a bee should be able to fly \ No newline at end of file From 5a6252a24a63eefe5b184882bc7a3ec514e892ee Mon Sep 17 00:00:00 2001 From: Sven Date: Thu, 9 Apr 2026 19:53:52 +0200 Subject: [PATCH 6/6] Remove unnecessary return --- src/fis_hook.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/fis_hook.c b/src/fis_hook.c index 016bd12..303c2b4 100644 --- a/src/fis_hook.c +++ b/src/fis_hook.c @@ -209,7 +209,6 @@ jlong JNICALL fis_skip0_hook(JNIEnv *env, jobject thiz, jlong n) { if (skip == remaining) { free(fd->data); fd->data = NULL; - return skip; } return skip;