Skip to content

fix(sdks/c): the length that crossed into C.GoBytes could store an empty object - #355

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/sdks-c-g115-audit-200
Aug 5, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/sdks-c-g115-audit-200

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #200.

What was wrong

objectfs_put takes a size_t and passed C.int(length) to C.GoBytes. size_t is 64 bits on every platform this library ships for and C.int is 32, so the length was truncated. Measured in a standalone cgo probe rather than reasoned about, because the same narrowing fails three different ways:

length = 1<<32     (4 GiB)  ->  C.int 0            ->  len(goData) == 0
length = (1<<32)+100        ->  C.int 100          ->  len(goData) == 100
length = 1<<31     (2 GiB)  ->  C.int -2147483648  ->  panic: gobytes: length out of range

The first is the dangerous one and is exactly this issue's stated concern — "a wrong-sized read with no error". A caller hands over 4 GiB, objectfs_put returns OBJECTFS_OK, and S3 holds an empty object. Nothing reports a short write, because from Go's side there was no short write: the length arrived as zero.

Confirmed end to end against real S3 (us-west-2) by removing the new guard and running tests/test_basic.c: the call returned OBJECTFS_OK, and aws s3api head-object on the key reported "ContentLength": 0.

The third case is worse in a shared library than in a program — a panic in a c-shared build tears down the host process, so a C caller loses unrelated state of its own to one bad argument. In the same run it aborted the test binary with exit 134 before stdout was flushed, so the harness printed nothing at all: not the section header, not a FAIL line, none of the 21 assertions that had already passed. That is written into the test's comment, because a regression here will look like the tests never ran rather than like a failure.

objectfs_list's negative limit is a second, smaller case of the same shape. The conversion was safe — C.int → int widens on every target this builds for. The value was not: ListObjects gates every use of limit on limit > 0, so -1 meant "return the whole bucket". That is what -1 conventionally means in a good deal of C API design, so it would have looked deliberate; it was an accident of a comparison, and nothing documented it.

Three findings that revise the issue's premises

1. gosec's line numbers were right all along. The issue and security.yml both say gosec "cannot map the location back to a real file", and the workflow dropped the findings on that basis. Half wrong — and the wrong half is the useful half. cgo emits //line directives into the file it generates, so each reported line is a real main.go line. Verified by inserting eight blank lines after the import block, which moved all three findings by exactly eight. Only the path is missing, and sdks/c is the only cgo package in the module, so there is exactly one path it can be.

So the filter now back-fills sdks/c/main.go rather than dropping the results. Scoped to results that have a region but no URI, so a genuinely malformed location elsewhere is still dropped rather than silently relabelled as living in sdks/c. This closes #200 "into the normal flow" as the issue hoped.

That matters because the alternative was measured, and it is not "these get reviewed somewhere else": golangci-lint discards the same findings for the same reason, and says so in its own log — runner/invalid_issue: issue related to file <go-build cache path> is skipped. Dropping them here left them visible to no tool at all. Three unreviewed integer conversions across a C ABI is how the defect above survived.

2. No gosec suppression directive works inside a cgo package. Probed at four placements — above the call, first of a comment block, last of a block, and trailing the call. In a pure-Go package all four suppress. In a cgo package none do, and gosec reports nosec: 0, meaning it recognized no directive at all. cgo rewrites each C.f(...) call into an inline closure carrying /*line :N:C*/ directives, which collapses the call and any nearby comment onto one synthetic position.

This is worth stating because I had first written the dual #nosec + //nolint:gosec annotation #264 calls for, and it does nothing here. Those were replaced with plain comments that do not claim to be doing something. It is also a caveat #264 should carry, since its premise is that the two runs read different directives — in a cgo package they read neither.

3. CGO_ENABLED=0 gosec ./sdks/c/... analyses zero files. The issue suggested this might resolve the paths. It reports zero issues over zero files, because the package cannot build without cgo — a vacuous pass, not a fix.

Audit result for every other conversion

Enumerated in the file's header comment, because "which of these can truncate" is not answerable from any one line. Lengths derived from a Go len (three sites) are genuinely unreachable; handle IDs are int64 on both sides; cache_bytes is C.long → int64, which widens or matches and never narrows.

Two errcheck findings fixed in passing: val.(*entry) was a bare type assertion in getEntry and objectfs_free. Neither can fail today, since nothing but that file writes to the sync.Map — but a bare assertion converts any future violation into a panic, and a panic here takes the host process down rather than just the call.

Tests

  • Go table tests for both guards at boundary values, including the three measured narrowing cases and MaxUint64, asserting that the message names both the length and the limit — objectfs_last_error is a C caller's only view of this, so a message saying merely "invalid length" is not actionable.
  • The load-bearing C-side test, which is the half the issue asked for: an oversized length is rejected rather than silently wrapped, checked through the real shared library on the path a C caller takes. main_test.go cannot import "C" (go test refuses it outright), so no Go test in this package can construct a size_t or call an exported function at all — hence both halves. Note no 4 GiB buffer is allocated: the guard runs before the pointer is dereferenced, which is what makes this testable.
  • make test-c already runs in CI (the sdk-gates job), and the integration halves self-skip without OBJECTFS_TEST_BUCKET, so these are gated on every PR and also verified against real S3 locally.

Verification

  • 4/4 mutations of the Go guards detected: guard removed, > → >= at the boundary, list guard removed, and the limit dropped from the message (5 assertions fire).
  • The decisive mutation: removing the guard reproduces both S3-visible failure modes — OBJECTFS_OK with ContentLength: 0, then the process abort.
  • The SARIF filter tested against the real 48-result document plus three injected negatives (no region, no locations, startLine: 0) — all three still dropped, all three real findings back-filled to correct lines. 48 results preserved where 45 survived before.
  • go test -race -timeout 20m ./... green across 38 packages · make test-c 21 passed / make test-python 15 passed · integration halves green against real S3 in us-west-2 · golangci-lint --new-from-merge-base=origin/main 0 issues · gofmt clean · go vet clean · security.yml re-parsed after editing · markdownlint at its 361 baseline.

One artifact of doing this work is now gitignored: go build ./sdks/c/ — the obvious way to check that package compiles — writes a 44 MB binary named c at the repo root.

…pty object

objectfs_put takes a size_t and passed C.int(length) to C.GoBytes. size_t is 64
bits on every platform this library ships for and C.int is 32, so the length was
truncated — and the same narrowing fails three different ways, measured in a
standalone cgo probe rather than reasoned about:

    length = 1<<32     (4 GiB)  ->  C.int 0            ->  len(goData) == 0
    length = (1<<32)+100        ->  C.int 100          ->  len(goData) == 100
    length = 1<<31     (2 GiB)  ->  C.int -2147483648  ->  panic

The first is the dangerous one, and it is exactly what #200 was concerned about:
a caller hands over 4 GiB, objectfs_put returns OBJECTFS_OK, and S3 holds an
empty object. Nothing reports a short write, because from Go's side there was no
short write — the length arrived as zero. Confirmed end to end against real S3 by
removing the new guard: the call returned OBJECTFS_OK and head-object on the key
reported "ContentLength": 0.

The third is worse in a shared library than in a program: a panic in a c-shared
build tears down the host process. In the same run it aborted the test binary
before stdout was flushed, so the harness printed nothing at all — not the 21
assertions that had already passed. Worth knowing in advance: a regression here
looks like the tests never ran.

objectfs_list's negative limit is a second, smaller case of the same shape. The
conversion was safe; the value was not. ListObjects gates every use of limit on
`limit > 0`, so -1 meant "the whole bucket" — which is what -1 conventionally
means in C API design, so it would have looked deliberate, but it was an accident
of a comparison and nothing documented it.

Both guards are now plain Go functions rather than inline ifs, because
main_test.go cannot import "C" (go test refuses it outright) and so cannot build
a C.size_t or call an exported function. The C side carries the half that proves
the return code and the error string actually arrive.

security.yml now back-fills the cgo SARIF path instead of dropping the findings.
This reverses a position stated in that file: gosec was said to be unable to map
the location back to a real file. Half wrong, and the wrong half is the useful
one — the line numbers are exactly right, because cgo emits //line directives
into the file it generates. Verified by inserting eight blank lines after the
import block, which moved all three findings by exactly eight. Only the path is
missing, and sdks/c is the only cgo package in the module.

Which matters because the alternative was measured and it is not "reviewed
elsewhere": golangci-lint discards the same findings for the same reason and says
so in its log — `runner/invalid_issue: issue related to file <cache path> is
skipped`. Three unreviewed integer conversions across a C ABI is how the defect
above survived.

Two further findings, recorded in main.go's header so they are not rediscovered:
no gosec suppression directive works inside a cgo package at all (probed at four
placements; all four suppress in pure Go, none do here, and gosec reports
nosec: 0), and `CGO_ENABLED=0 gosec ./sdks/c/...` — which #200 suggested might
resolve the paths — analyses zero files, so its clean report is vacuous.

Verified: 4/4 mutations of the two Go guards detected; the guard's removal
reproduces both S3-visible failure modes; go test -race ./... green across 38
packages; make test-c and make test-python pass, and the integration halves pass
against real S3 in us-west-2; golangci-lint 0 issues; the SARIF filter tested
against the real 48-result document plus three injected negatives (no region, no
locations, startLine 0), all three still dropped.

Refs #200
@scttfrdmn
scttfrdmn merged commit f6f2ae5 into main Aug 5, 2026
33 checks passed
@scttfrdmn
scttfrdmn deleted the fix/sdks-c-g115-audit-200 branch August 5, 2026 22:18
scttfrdmn added a commit that referenced this pull request Aug 6, 2026
… (#372)

pkg/archive had no tests. Its 48.6% was measured through internal/archive under
-coverpkg, which is a number about a consumer, and the code no consumer touches
was invisible in it. metadata_test.go takes the package to 100.0%.

#360 offered testing directly or folding into the consumer, and the premise had
moved since it was filed: internal/archive now builds an index and reads it back,
so the genuinely unreached code was ListDirectory and its two helpers, which no
consumer calls at all. Only the first option covers them.

Reaching 100% found a branch that is unreachable from the only producer here, and
it is covered with that written down rather than deleted. ListDirectory's second
disjunct needs an entry whose Path ends in '/', and BuildIndexFromBytes runs every
tar name through path.Clean, which strips exactly that. ArchiveIndex is exported,
so an outside caller may build an index using tar's own convention where a
directory keeps its slash; the test says which convention each half serves, so
removing the branch stays a decision about the exported surface. Two behaviors are
pinned rather than assumed: TotalFiles counts directories and TotalSize does not,
and a repeated path replaces the entry without rolling either counter back.

For #361 the coverage job now states CGO_ENABLED: 1. It set nothing, so it took
the runner default, and sdks/c is the one package whose coverage depends on it — a
floor read off an inherited value is a claim about a configuration no workflow
states. On rather than off: main.go carries `import "C"`, so with cgo off the
package does not fall back to a tag-excluded variant, it fails to compile, because
main_test.go references codeFromErr, fillCStr, maxPutLength and more. CGO_ENABLED=0
would turn a coverage question into a compile failure in the job whose output is a
coverage number.

The sdks/c floor is 12, not the 11 this file claimed while arguing the package was
unfloorable. That number was stale when written: #355 added the tests taking it to
12.1%, and the note was authored on a branch cut before #355 merged, so it recorded
a measurement its own commit had invalidated. Verified rather than reasoned — no
commit has touched sdks/c since, and it measures 11.0% at f6f2ae5^ and 12.1% at
HEAD. The floor is a ratchet on the Go-testable half; the rest is //export entry
points taking C pointers, which the C suite exercises and go test cannot.

Also: the gate stops measuring Go files vendored inside npm dependencies. Two
copies of flatted ship a Go port, so after npm install `go list ./...` reports two
more packages and the gate named them unfloored at 0.0%. Gitignored, absent from a
fresh checkout, never installed by the coverage job — so visible only on a
developer machine, and the fix is neither a floor nor a test because the code is
not ours. TestUnfloorablePackagesAreExplained used to filter them out of its own
synthetic profile, which passed the test and left the noise in the output a person
reads; that filter is gone, so the profile names them and the gate's exclusion is
what the assertion tests.

Verified by mutation, both new gates: dropping the node_modules skip fails
TestUnfloorablePackagesAreExplained naming both packages, and a floor of 13 fails
sdks/c at 12.1%.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sdks/c: audit the G115 integer conversions that SARIF now drops — nothing else tracks them

1 participant