Title: Data.Int.toStringAs leaks a global `n` (missing `local`) · Issue #1 · purescript-lua/purescript-lua-integers · GitHub
Open Graph Title: Data.Int.toStringAs leaks a global `n` (missing `local`) · Issue #1 · purescript-lua/purescript-lua-integers
X Title: Data.Int.toStringAs leaks a global `n` (missing `local`) · Issue #1 · purescript-lua/purescript-lua-integers
Description: Summary src/Data/Int.lua declares the loop variable n in toStringAs without local, so it leaks into the global environment. luacheck --std min src/ flags it as setting non-standard global variable n, and because the rest of the function ...
Open Graph Description: Summary src/Data/Int.lua declares the loop variable n in toStringAs without local, so it leaks into the global environment. luacheck --std min src/ flags it as setting non-standard global variable ...
X Description: Summary src/Data/Int.lua declares the loop variable n in toStringAs without local, so it leaks into the global environment. luacheck --std min src/ flags it as setting non-standard global variable ...
Opengraph URL: https://github.com/purescript-lua/purescript-lua-integers/issues/1
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Data.Int.toStringAs leaks a global `n` (missing `local`)","articleBody":"## Summary\n\n`src/Data/Int.lua` declares the loop variable `n` in `toStringAs` without `local`, so it leaks into the global environment. `luacheck --std min src/` flags it as `setting non-standard global variable n`, and because the rest of the function reads that name, the single missing `local` cascades into eight separate warnings.\n\n## Offending code\n\n```lua\ntoStringAs = (function(radix)\n return function(i)\n local floor, insert = math.floor, table.insert\n n = floor(i) -- \u003c-- no `local`: global leak\n if radix == 10 then return tostring(n) end\n ...\n if n \u003c 0 then\n sign = \"-\"\n n = -n\n end\n repeat\n local d = (n % radix) + 1\n n = floor(n / radix)\n insert(t, 1, digits:sub(d, d))\n until n == 0\n ...\n```\n\n`floor` and `insert` on the preceding line are correctly `local`; only `n` was missed.\n\n## Root cause\n\nA plain assignment `n = floor(i)` instead of `local n = floor(i)`. In Lua an unqualified assignment to an undeclared name writes to the global table `_G`. Every later use of `n` in the function then resolves to that global rather than a local.\n\n## Impact\n\n- Functional: each call to `Data.Int.toStringAs` mutates the shared global `n`. In a single-threaded Lua program this is usually benign, but it is an unintended global write that can collide with any other code (the FFI runtime, user code, another module) that happens to use a global named `n`, producing hard-to-trace cross-module interference. It is the kind of latent footgun that only manifests under specific call interleavings.\n- Hygiene/tooling: `luacheck --quiet --std min src/` exits non-zero on this file. The one missing `local` produces these warnings:\n\n```\nsrc/Data/Int.lua:25:7: setting non-standard global variable n\nsrc/Data/Int.lua:26:43: accessing undefined variable n\nsrc/Data/Int.lua:30:10: accessing undefined variable n\nsrc/Data/Int.lua:32:9: setting non-standard global variable n\nsrc/Data/Int.lua:32:14: accessing undefined variable n\nsrc/Data/Int.lua:35:20: accessing undefined variable n\nsrc/Data/Int.lua:36:9: setting non-standard global variable n\nsrc/Data/Int.lua:36:19: accessing undefined variable n\n```\n\nAll eight collapse once `n` is declared `local`.\n\n## How it was found\n\nLinting the FFI sources of the Lua-fork package with `luacheck --quiet --std min src/` while auditing the `purescript-lua` package ecosystem for Lua-5.1/global-hygiene issues.\n\n## Fix\n\nAdd `local` to the declaration of `n`:\n\n```lua\nlocal n = floor(i)\n```\n\nMinimal, single-line, `src`-only change. No behaviour change for any well-formed program; it merely confines `n` to the function scope where it belongs.\n\n## Note on out-of-scope warnings\n\nThe same file also reports `accessing undefined field pow of global math` (`math.pow`, removed in Lua 5.2+) and an `unused argument nothing`. Those are distinct pre-existing issues and are intentionally left out of this fix to keep the change minimal and focused on the global leak.\n\n## Cross-reference\n\nTracked upstream by pslua issue Unisay/purescript-lua#51; this is the detailed package-level report it points to.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-06-14T08:31:20Z","url":"https://github.com/purescript-lua/purescript-lua-integers/issues/1"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:470b3b3f-d8d2-a532-c2aa-3d522b733939 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D482:27E5CD:A67DE3:D84F07:6B00E5A8 |
| html-safe-nonce | 0b5e252ed9d5763f42ca2c62ae4c815300078d801c8705588ed8d1933dce7732 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENDgyOjI3RTVDRDpBNjdERTM6RDg0RjA3OjZCMDBFNUE4IiwidmlzaXRvcl9pZCI6IjM3NjM0NzgyMzMyMjAxNzkzNjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | eba7b545f24266cae526579fa1faf9aa5a42b81f8994235a93d7fa054fb8f050 |
| hovercard-subject-tag | issue:4658448293 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/purescript-lua/purescript-lua-integers/1/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ad2a1df2dc0035c284947a8d8d251e62bbef6ed9f2ff59f61523e05fe6009b92/purescript-lua/purescript-lua-integers/issues/1 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ad2a1df2dc0035c284947a8d8d251e62bbef6ed9f2ff59f61523e05fe6009b92/purescript-lua/purescript-lua-integers/issues/1 |
| og:image:alt | Summary src/Data/Int.lua declares the loop variable n in toStringAs without local, so it leaks into the global environment. luacheck --std min src/ flags it as setting non-standard global variable ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Unisay |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| go-import | github.com/purescript-lua/purescript-lua-integers git https://github.com/purescript-lua/purescript-lua-integers.git |
| octolytics-dimension-user_id | 131581790 |
| octolytics-dimension-user_login | purescript-lua |
| octolytics-dimension-repository_id | 779795746 |
| octolytics-dimension-repository_nwo | purescript-lua/purescript-lua-integers |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | true |
| octolytics-dimension-repository_parent_id | 30888599 |
| octolytics-dimension-repository_parent_nwo | purescript/purescript-integers |
| octolytics-dimension-repository_network_root_id | 30888599 |
| octolytics-dimension-repository_network_root_nwo | purescript/purescript-integers |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width