diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 53417f6..9b6038e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -246,6 +246,47 @@ jobs: exit 1 fi + # A NAME IS COMPARED IN PLACE (0.15.1): a variable whose name is 300 bytes, set by the C + # library, is found by kal_env_var. Until 0.15.0 the name was copied into 256 bytes and a + # longer one was refused with kal_err_invalid; and kal_env_var_at answered the whole entry + # where it answers the name. + - name: A variable with a name of 300 bytes is found by its name, and a name by its position + run: | + set -euo pipefail + work="$RUNNER_TEMP/longname" + rm -rf "$work"; mkdir -p "$work/src" + impl="$(pwd)" + cat > "$work/mcpp.toml" << TOML + [package] + name = "longname" + version = "0.1.0" + + [dependencies] + openkal = { path = "$impl/.spec" } + openkal-emscripten = { path = "$impl" } + TOML + cat > "$work/src/main.cpp" << 'CPP' + #include + #include + import openkal.env; + int main() { + static char name[301]; + memset(name, 'N', 300); + if (setenv(name, "long", 1) != 0) return 2; + char v[8]; + const kal_intptr n = kal_env_var(name, 300, v, sizeof v); + if (!(n == 4 && memcmp(v, "long", 4) == 0)) return 1; + // The name at a position, which is found again by that name. + char first[512]; + const kal_intptr f = kal_env_var_at(0, first, sizeof first); + if (f <= 0 || f >= (kal_intptr)sizeof first || memchr(first, '=', (size_t)f)) return 3; + return kal_env_var(first, (kal_uintptr)f, nullptr, 0) >= 0 ? 0 : 4; + } + CPP + ( cd "$work" && timeout 300 mcpp run --target wasm32-emscripten ) \ + || { echo "::error::a variable with a name of 300 bytes was not found by its name"; exit 1; } + echo " ok a variable with a name of 300 bytes is found by its name" + - name: Point at the specification's working tree run: | # run-conformance.sh has already done this; it is repeated because a diff --git a/README.md b/README.md index 423671e..b741046 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,32 @@ translation is most of each function; what an above-libc implementation has to get right is the places where the C library's vocabulary and openkal's do **not** correspond, and those are the places the sources comment individually. +## What 0.15.1 corrects + +`kal_env_var` compares the name it is given with each entry of `environ` in +place. Until 0.15.0 it copied the name into a buffer of 256 bytes for `getenv`, +and refused a longer name with `kal_err_invalid`: a variable the environment +holds, enumerates through `kal_env_var_at` and passes on could not be looked up +by its name, a bound of the implementation's own below the environment's +(clause 7.6 states the same of a start). Measured under node: a variable with a +name of 300 bytes, set by `setenv`, is found by 0.15.1 and refused by 0.15.0. CI +runs that program. + +`kal_env_var_at` answers the name at a position, as `openkal/env.h` states. Until +0.15.0 it answered the whole `NAME=value`, which `kal_env_var` then did not find; +the specification's suite of openkal 0.26 observes the pair (its first +variable, looked up again by its name). + +## What 0.15 changes (openkal 0.26) + +Nothing but the specification's version: the manifest declares `openkal = +"^0.26.0"`. openkal 0.26 requires two things of `openkal.process`, a start +bounded by the environment alone (clause 7.6) and a unit claimed only where a +reference can be made (clause 7.18), and this implementation does not provide +that interface, so neither `kal_process_spawn` nor `kal_process_job_create` is +defined. The arguments and the variables a program receives are read from the +host's vector and from `environ` at each enquiry; no table bounds their number. + ## What 0.14.3 corrects The descriptor-rights tables (4,096 entries and 64 roots) had members whose @@ -254,10 +280,10 @@ implementation in its source: ```toml [dependencies] -openkal = "0.25.0" +openkal = "0.26.0" [target.'cfg(os = "emscripten")'.dependencies] -openkal-emscripten = "0.14.3" +openkal-emscripten = "0.15.1" ``` ## Licence diff --git a/mcpp.toml b/mcpp.toml index 7f6334f..544e8e1 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -1,7 +1,7 @@ [package] namespace = "mcpplibs" name = "openkal-emscripten" -version = "0.14.3" +version = "0.15.1" description = "An implementation of openkal for Emscripten, written ABOVE a C library rather than beneath one, because on this platform there is no kernel to issue calls to." license = "Apache-2.0" @@ -25,7 +25,7 @@ repo = "https://github.com/mcpplibs/openkal-emscripten" [dependencies] # Every patch release of the specification's minor version (openkal clause 8). -openkal = "^0.25.0" +openkal = "^0.26.0" [build] # THE DIALECT FLAGS ARE THE SAME AS EVERY OTHER IMPLEMENTATION'S AND THE REASON diff --git a/src/env.cpp b/src/env.cpp index 5d50c37..62afa51 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -164,20 +164,25 @@ kal_intptr kal_env_arg(kal_uintptr index, char* out, kal_uintptr cap) { return copy_out(name, ::strlen(name), out, cap); } -// `getenv` TAKES A NUL-TERMINATED NAME AND openkal PASSES A LENGTH, so the -// name is copied. Bounded rather than allocated: a variable name longer than -// this is not a name, and an allocation here would make a lookup able to fail -// for want of memory. +// A NAME IS COMPARED IN PLACE, against each entry of `environ' (0.15.1). `getenv' takes a +// terminated name and openkal passes a length, and until 0.15.0 the name was copied into a buffer of +// 256 bytes, so a variable whose name was longer --- which the environment holds, enumerates and +// passes on --- was refused with kal_err_invalid: a bound of this implementation's own below the +// environment's. Comparing the entries is what `getenv' does, and needs neither the copy nor an +// allocation that could fail a lookup for want of memory. kal_intptr kal_env_var(const char* name, kal_uintptr name_len, char* out, kal_uintptr cap) { - if (name == nullptr) return -kal_err_invalid; - char key[256]; - if (name_len == 0 || name_len >= sizeof(key)) return -kal_err_invalid; - ::memcpy(key, name, static_cast(name_len)); - key[name_len] = '\0'; - const char* v = ::getenv(key); - if (v == nullptr) return -kal_err_not_found; - return copy_out(v, ::strlen(v), out, cap); + extern char** environ; + if (name == nullptr || name_len == 0) return -kal_err_invalid; + for (char** e = environ; e != nullptr && *e != nullptr; ++e) { + const char* entry = *e; + kal_uintptr k = 0; + while (k < name_len && entry[k] != '\0' && entry[k] == name[k]) ++k; + if (k != name_len || entry[k] != '=') continue; + const char* v = entry + k + 1; + return copy_out(v, ::strlen(v), out, cap); + } + return -kal_err_not_found; } // `environ` IS THE ENUMERATION, AND IT IS THE ONLY ONE. POSIX offers no @@ -197,8 +202,13 @@ kal_intptr kal_env_var_at(kal_uintptr index, char* out, kal_uintptr cap) { kal_uintptr n = 0; while (environ[n] != nullptr) ++n; if (index >= n) return -kal_err_not_found; + // THE NAME, NOT THE ENTRY (0.15.1). openkal.env answers a name at a position and the value + // through kal_env_var; until 0.15.0 this answered the whole `NAME=value', which looked up + // again by kal_env_var was not found (the specification's suite observes the pair). const char* s = environ[index]; - return copy_out(s, ::strlen(s), out, cap); + kal_uintptr k = 0; + while (s[k] != '\0' && s[k] != '=') ++k; + return copy_out(s, k, out, cap); } // THE IMAGE, THE LOADER AND THE SYSTEM (version 0.17).