Skip to content

Commit 35f2046

Browse files
committed
Fix returning GETENV_ERR_LENGTH for over-long strings
1 parent 56d4f8f commit 35f2046

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

shared-module/os/getenv.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ STATIC void seek_eof(file_arg *active_file) {
8686
STATIC void vstr_add_byte_nonstd(vstr_t *vstr, byte b) {
8787
if (!vstr->fixed_buf || vstr->alloc > vstr->len) {
8888
vstr_add_byte(vstr, b);
89+
} else {
90+
vstr->len++;
8991
}
9092
}
9193

@@ -97,6 +99,8 @@ STATIC void vstr_add_char_nonstd(vstr_t *vstr, unichar c) {
9799
(c < 0x10000) ? 3 : 4;
98100
if (!vstr->fixed_buf || vstr->alloc > vstr->len + ulen) {
99101
vstr_add_char(vstr, c);
102+
} else {
103+
vstr->len += ulen;
100104
}
101105
}
102106

@@ -297,7 +301,7 @@ STATIC os_getenv_err_t os_getenv_buf_terminated(const char *key, char *value, si
297301
if (result == GETENV_OK) {
298302
vstr_add_byte_nonstd(&buf, 0);
299303
memcpy(value, buf.buf, MIN(buf.len, value_len));
300-
if (buf.len > value_len) {
304+
if (buf.len > value_len) { // this length includes trailing NUL
301305
result = GETENV_ERR_LENGTH;
302306
}
303307
}

0 commit comments

Comments
 (0)