PrimeIntellect/SWE-rebench-V2 huggingface dataset:
Repository: google/go-tpm (go)

Issue:
Should unpack of U16/U32Bytes validate a max size?
Malicious attacker-controlled input could make its way to `tpmutil.Unpack()` of `U32Bytes` or `U16Bytes`. For instance, calling `tpm.UnmarshalPubRSAPublicKey()` with `20ff ffff 2020 20ff ffff 2020` will cause a massive allocation, crashing the machine.

Given that `TPMUnmarshal` takes a reader, theres no upfront knowledge of the size of the buffer being processed.

Anyone have an opinion on these options?

1. Add a max size (maybe 1mb?) which is larger than all expected use-cases
2. Refactor `tpmutil.Unpack` to pass the size of the buffer down the chain so size prefix fields can be properly validated.

WDYT?

Pull request:
Add bounds checks for unpacks into U32/U16 bytes
Fixes #154 

Patch:
diff --git a/tpmutil/structures.go b/tpmutil/structures.go
index c67b51a..d7d05fb 100644
--- a/tpmutil/structures.go
+++ b/tpmutil/structures.go
@@ -15,11 +15,19 @@
 package tpmutil
 
 import (
+	"bytes"
 	"encoding/binary"
 	"fmt"
 	"io"
 )
 
+// maxBytesBufferSize sets a sane upper bound on the size of a U32Bytes
+// buffer. This limit exists to prevent a maliciously large size prefix
+// from resulting in a massive memory allocation, potentially causing
+// an OOM condition on the system.
+// We expect no buffer from a TPM to approach 1Mb in size.
+const maxBytesBufferSize = 1024 * 1024 // 1Mb.
+
 // RawBytes is for Pack and RunCommand arguments that are already encoded.
 // Compared to []byte, RawBytes will not be prepended with slice length during
 // encoding.
@@ -52,6 +60,7 @@ func (b *U16Bytes) TPMUnmarshal(in io.Reader) error {
 		return err
 	}
 	size := int(tmpSize)
+
 	if len(*b) >= size {
 		*b = (*b)[:size]
 	} else {
@@ -63,7 +72,7 @@ func (b *U16Bytes) TPMUnmarshal(in io.Reader) error {
 		return err
 	}
 	if n != size {
-		return fmt.Errorf("unable to read all contents in to U16Bytes")
+		return io.ErrUnexpectedEOF
 	}
 	return nil
 }
@@ -95,6 +104,11 @@ func (b *U32Bytes) TPMUnmarshal(in io.Reader) error {
 		return err
 	}
 	size := int(tmpSize)
+
+	if size > maxBytesBufferSize {
+		return bytes.ErrTooLarge
+	}
+
 	if len(*b) >= size {
 		*b = (*b)[:size]
 	} else {


Test patch:
diff --git a/tpmutil/encoding_test.go b/tpmutil/encoding_test.go
index 4cf4aa1..9b4238e 100644
--- a/tpmutil/encoding_test.go
+++ b/tpmutil/encoding_test.go
@@ -364,3 +364,17 @@ func TestUnpackHandlesArea(t *testing.T) {
 		t.Errorf("Unpack(%v, %T): %T = %v, want %v", buf, &out, out, out, want)
 	}
 }
+
+func TestUnpackMalformedBytes(t *testing.T) {
+	// buf is malformed because the leading size prefix is illegally large.
+	buf := []byte{0xff, 0xff, 0xff, 0xff, 0x20}
+
+	var u32 U32Bytes
+	if _, err := Unpack(buf, &u32); err != bytes.ErrTooLarge {
+		t.Errorf("Unpack(U32Bytes{}) returned %q, want %q", err, bytes.ErrTooLarge)
+	}
+	var u16 U16Bytes
+	if _, err := Unpack(buf, &u16); err != io.ErrUnexpectedEOF {
+		t.Errorf("Unpack(U16Bytes{}) returned %q, want %q", err, io.ErrUnexpectedEOF)
+	}
+}
Repository: polarsignals/frostdb (go)

Issue:
SeekToRow skips rows during compaction
```
thor@thors-MacBook-Pro frostdb % go test -v -run XXX -bench Writers
goos: darwin
goarch: arm64
pkg: github.com/polarsignals/frostdb
Benchmark_Table_Insert_1000Rows_10Iters_10Writers
    table_test.go:427:
        	Error Trace:	table_test.go:427
        	            				table_test.go:307
        	Error:      	Not equal:
        	            	expected: 100000
        	            	actual  : 55634
        	Test:       	Benchmark_Table_Insert_1000Rows_10Iters_10Writers
--- FAIL: Benchmark_Table_Insert_1000Rows_10Iters_10Writers
```

After a git bisect it appears that https://github.com/polarsignals/frostdb/commit/017d5f6d9baff694ad147cdd15071feb367f71a0 is the offending commit @asubiotto 

Pull request:
frostdb: remove unnecessary SeekToRow call during compaction
This call would simply verify that we placed the cursor at the desired row. However, this SeekToRow is causing correctness issues although it's supposed to be a noop.

Fixes #294 

Patch:
diff --git a/compaction.go b/compaction.go
index 45b1674..96b9989 100644
--- a/compaction.go
+++ b/compaction.go
@@ -566,9 +566,6 @@ func compactLevel0IntoLevel1(
 
 		for {
 			var mergedBytes bytes.Buffer
-			if err := rows.SeekToRow(int64(cursor)); err != nil {
-				return err
-			}
 			n, err := t.writeRows(&mergedBytes, rows, merged.DynamicColumns(), estimatedRowsPerPart)
 			if err != nil {
 				return err


Test patch:
diff --git a/compaction_test.go b/compaction_test.go
index 5852e59..d762fbf 100644
--- a/compaction_test.go
+++ b/compaction_test.go
@@ -216,6 +216,46 @@ func TestCompaction(t *testing.T) {
 				},
 			},
 		},
+		{
+			name: "MultipleL0ToL1",
+			inserts: []int64{
+				acc, 1, 2, 3, flushAcc,
+				acc, 4, 5, 6, flushAcc,
+				recordGranuleSizeCommand,
+				acc, 7, 8, 9, flushAcc,
+				setRecordedGranuleSizeCommand,
+				compactCommand,
+			},
+			expected: []expectedGranule{
+				{
+					[]expectedPart{
+						{
+							numRowGroups:    2,
+							compactionLevel: compactionLevel1,
+							data:            []int64{1, 2, 3, 4},
+						},
+					},
+				},
+				{
+					[]expectedPart{
+						{
+							numRowGroups:    2,
+							compactionLevel: compactionLevel1,
+							data:            []int64{5, 6, 7, 8},
+						},
+					},
+				},
+				{
+					[]expectedPart{
+						{
+							numRowGroups:    1,
+							compactionLevel: compactionLevel1,
+							data:            []int64{9},
+						},
+					},
+				},
+			},
+		},
 	}
 
 	numParts := func(g *Granule) int {
diff --git a/table_test.go b/table_test.go
index b0a5241..31140fb 100644
--- a/table_test.go
+++ b/table_test.go
@@ -334,6 +334,7 @@ func benchmarkTableInserts(b *testing.B, rows, iterations, writers int) {
 		WithStoragePath(dir),
 	)
 	require.NoError(b, err)
+	defer c.Close()
 
 	db, err := c.DB(context.Background(), "test")
 	require.NoError(b, err)