nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: C2FO/vfs
Pull request #70 (2021-05-10 17:58:21)

Fix: do not offset number of bytes read for memfs
Previously memfs backend's Read would correctly assign the data read
from a file when its cursor position was not at 0, but the returned n
int was incorrectly offset by the original cursor position.

Add a test for successive reads on memfs file.

--------------
Hi there, we recently started with your project and noticed this bug when testing partial file reads using memfs.
I have included the fix and a new test case to cover it. Please do feel free to let me know if there's anything I have missed, definitely happy to take suggestions as this is my first contribution to the project. Thanks!


Patch:
diff --git a/backend/mem/file.go b/backend/mem/file.go
index b66bc65..2934570 100644
--- a/backend/mem/file.go
+++ b/backend/mem/file.go
@@ -93,7 +93,6 @@ func (f *File) Read(p []byte) (n int, err error) {
 		return 0, doesNotExist()
 	}
 	// if file exists:
-	offset := f.cursor
 	if !f.isOpen {
 		f.isOpen = true
 	}
@@ -130,7 +129,7 @@ func (f *File) Read(p []byte) (n int, err error) {
 	if f.cursor > len(f.contents) {
 		f.cursor = len(f.contents)
 	}
-	return readBufferLength - offset, nil
+	return readBufferLength, nil
 
 }
 


Test patch:
diff --git a/backend/mem/file_test.go b/backend/mem/file_test.go
index fc72274..de53001 100644
--- a/backend/mem/file_test.go
+++ b/backend/mem/file_test.go
@@ -1,6 +1,7 @@
 package mem
 
 import (
+	"io"
 	"io/ioutil"
 	"log"
 	"os"
@@ -603,6 +604,36 @@ func (s *memFileTest) TestWrite() {
 
 }
 
+// TestRead ensures read can be called successively to get an entire file's contents in chunks
+func (s *memFileTest) TestRead() {
+	expectedSlice := []byte("Hello World!")
+	length := len(expectedSlice)
+	fileToRead, err := s.fileSystem.NewFile("", "/fileToRead.txt")
+	s.NoError(err, "unexpected new file error")
+	num, err := fileToRead.Write(expectedSlice)
+	s.NoError(err, "unexpected write error")
+	s.EqualValues(length, num)
+	s.NoError(fileToRead.Close(), "close error not expected")
+
+	chunk1 := make([]byte, 5)
+	num, err = fileToRead.Read(chunk1)
+	s.NoError(err, "unexpected read error")
+	s.EqualValues(5, num)
+	s.EqualValues(expectedSlice[:5], chunk1)
+
+	chunk2 := make([]byte, 5)
+	num, err = fileToRead.Read(chunk2)
+	s.NoError(err, "unexpected read error")
+	s.EqualValues(5, num)
+	s.EqualValues(expectedSlice[5:10], chunk2)
+
+	chunk3 := make([]byte, 5)
+	num, err = fileToRead.Read(chunk3)
+	s.EqualValues(io.EOF, err)
+	s.EqualValues(2, num)
+	s.EqualValues(expectedSlice[10:], chunk3[:2])
+}
+
 // TestWriteThenReadNoClose writes to a file, and reads from it without closing it by seeking to the start
 func (s *memFileTest) TestWriteThenReadNoClose() {
 	expectedText := "new file"
Repository: BurntSushi/toml
Pull request #276 (2021-06-08 06:12:18)

Detect UTF-16 files and give a more helpful error
The TOML specification states that "A TOML file must be a valid UTF-8
encoded Unicode document", but especially on Windows it's easy to mix
this up as it calls "UTF-16" just "Unicode".

Most UTF-16 files have a BOM, so detect this and error out on it. Do
this before the lexing as lexer.next() assumes UTF-8 and mangles stuff.

Some files don't have a BOM; if a file contains NULL bytes then this is
probably the second byte in the surrogate pair.


Patch:
diff --git a/lex.go b/lex.go
index e0a742a..37ef779 100644
--- a/lex.go
+++ b/lex.go
@@ -389,6 +389,10 @@ func lexBareKey(lx *lexer) stateFn {
 		lx.emit(itemText)
 		return lexKeyEnd
 	default:
+		// NULL bytes probably means it's a UTF-16 file without BOM.
+		if r == 0 {
+			return lx.errorf("bare keys cannot contain %q; probably using UTF-16; TOML files must be UTF-8", r)
+		}
 		return lx.errorf("bare keys cannot contain %q", r)
 	}
 }
diff --git a/parse.go b/parse.go
index c0be76f..e5025b7 100644
--- a/parse.go
+++ b/parse.go
@@ -47,6 +47,12 @@ func parse(data string) (p *parser, err error) {
 		}
 	}()
 
+	if strings.HasPrefix(data, "\xff\xfe") || strings.HasPrefix(data, "\xfe\xff") {
+		return nil, fmt.Errorf(
+			"document starts with UTF-16 byte-order-mark (BOM) 0x%x; TOML files must be UTF-8",
+			data[:2])
+	}
+
 	p = &parser{
 		mapping:   make(map[string]interface{}),
 		types:     make(map[string]tomlType),


Test patch:
diff --git a/decode_test.go b/decode_test.go
index 95a5ba6..511ce08 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -88,6 +88,42 @@ cauchy = "cat 2"
 	}
 }
 
+func TestUTF16(t *testing.T) {
+	tests := []struct {
+		in      []byte
+		wantErr string
+	}{
+		// a = "b" in UTF-16, without BOM and with the LE and BE BOMs.
+		{
+			[]byte{0x61, 0x00, 0x20, 0x00, 0x3d, 0x00, 0x20, 0x00, 0x22, 0x00, 0x62, 0x00, 0x22, 0x00, 0x0a, 0x00},
+			`bare keys cannot contain '\x00'; probably using UTF-16; TOML files must be UTF-8`,
+		},
+		{
+			[]byte{0xfe, 0xff, 0x61, 0x00, 0x20, 0x00, 0x3d, 0x00, 0x20, 0x00, 0x22, 0x00, 0x62, 0x00, 0x22, 0x00, 0x0a, 0x00},
+			`document starts with UTF-16 byte-order-mark (BOM) 0xfeff; TOML files must be UTF-8`,
+		},
+		{
+			[]byte{0xff, 0xfe, 0x61, 0x00, 0x20, 0x00, 0x3d, 0x00, 0x20, 0x00, 0x22, 0x00, 0x62, 0x00, 0x22, 0x00, 0x0a, 0x00},
+			`document starts with UTF-16 byte-order-mark (BOM) 0xfffe; TOML files must be UTF-8`,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run("", func(t *testing.T) {
+			var s struct {
+				A string
+			}
+			_, err := Decode(string(tt.in), &s)
+			if err == nil {
+				t.Fatal("err is nil")
+			}
+			if !strings.Contains(err.Error(), tt.wantErr) {
+				t.Errorf("wrong error\nhave: %q\nwant: %q", err, tt.wantErr)
+			}
+		})
+	}
+}
+
 func TestDecodeEmbedded(t *testing.T) {
 	type Dog struct{ Name string }
 	type Age int
Repository: BurntSushi/toml
Pull request #278 (2021-06-09 11:20:01)

Export ParseError so the detailed information is available for callers
Adapted from #264 with a test added


Patch:
diff --git a/parse.go b/parse.go
index 1b44307..89a251b 100644
--- a/parse.go
+++ b/parse.go
@@ -31,17 +31,24 @@ type parser struct {
 	implicits map[string]bool
 }
 
-type parseError string
+// ParseError is used when a file can't be parsed: for example invalid integer
+// literals, duplicate keys, etc.
+type ParseError struct {
+	Message string
+	Line    int
+	LastKey string
+}
 
-func (pe parseError) Error() string {
-	return string(pe)
+func (pe ParseError) Error() string {
+	return fmt.Sprintf("Near line %d (last key parsed '%s'): %s",
+		pe.Line, pe.LastKey, pe.Message)
 }
 
 func parse(data string) (p *parser, err error) {
 	defer func() {
 		if r := recover(); r != nil {
 			var ok bool
-			if err, ok = r.(parseError); ok {
+			if err, ok = r.(ParseError); ok {
 				return
 			}
 			panic(r)
@@ -83,9 +90,12 @@ func parse(data string) (p *parser, err error) {
 }
 
 func (p *parser) panicf(format string, v ...interface{}) {
-	msg := fmt.Sprintf("Near line %d (last key parsed '%s'): %s",
-		p.approxLine, p.current(), fmt.Sprintf(format, v...))
-	panic(parseError(msg))
+	msg := fmt.Sprintf(format, v...)
+	panic(ParseError{
+		Message: msg,
+		Line:    p.approxLine,
+		LastKey: p.current(),
+	})
 }
 
 func (p *parser) next() item {


Test patch:
diff --git a/decode_test.go b/decode_test.go
index 0caf3f6..983d141 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -1,6 +1,7 @@
 package toml
 
 import (
+	"errors"
 	"fmt"
 	"log"
 	"math"
@@ -1557,6 +1558,39 @@ cauchy = "cat 2"
 	}
 }
 
+func TestParseError(t *testing.T) {
+	file :=
+		`a = "a"
+b = "b"
+c = 001  # invalid
+`
+
+	var s struct {
+		A, B string
+		C    int
+	}
+	_, err := Decode(file, &s)
+	if err == nil {
+		t.Fatal("err is nil")
+	}
+
+	var pErr ParseError
+	if !errors.As(err, &pErr) {
+		t.Fatalf("err is not a ParseError: %T %[1]v", err)
+	}
+
+	want := ParseError{
+		Line:    3,
+		LastKey: "c",
+		Message: `Invalid integer "001": cannot have leading zeroes`,
+	}
+	if !strings.Contains(pErr.Message, `Invalid integer "001"`) ||
+		pErr.Line != want.Line ||
+		pErr.LastKey != want.LastKey {
+		t.Errorf("unexpected data\nhave: %#v\nwant: %#v", pErr, want)
+	}
+}
+
 // errorContains checks if the error message in have contains the text in
 // want.
 //
Repository: BurntSushi/toml
Pull request #280 (2021-06-09 14:15:37)

Better error when decoding to a map without string keys
Previously it would panic with "reflect: call of reflect.Value.SetString
on int Value", which wasn't very good.

There's already a check for this when encoding, so add one for decoding
as well.

Closes #216


Patch:
diff --git a/decode.go b/decode.go
index b062704..472c698 100644
--- a/decode.go
+++ b/decode.go
@@ -270,6 +270,12 @@ func (md *MetaData) unifyStruct(mapping interface{}, rv reflect.Value) error {
 }
 
 func (md *MetaData) unifyMap(mapping interface{}, rv reflect.Value) error {
+	if k := rv.Type().Key().Kind(); k != reflect.String {
+		return fmt.Errorf(
+			"toml: cannot decode to a map with non-string key type (%s in %q)",
+			k, rv.Type())
+	}
+
 	tmap, ok := mapping.(map[string]interface{})
 	if !ok {
 		if tmap == nil {


Test patch:
diff --git a/decode_test.go b/decode_test.go
index 983d141..698bd3d 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -641,24 +641,27 @@ func TestDecodeMalformedNumbers(t *testing.T) {
 	}
 }
 
-func TestDecodeBadValues(t *testing.T) {
+func TestDecodeTypes(t *testing.T) {
+	type mystr string
+
 	for _, tt := range []struct {
 		v    interface{}
 		want string
 	}{
+		{new(map[string]int64), ""},
+		{new(map[mystr]int64), ""},
+
 		{3, "non-pointer int"},
 		{(*int)(nil), "nil"},
+		{new(map[int]string), "cannot decode to a map with non-string key type"},
+		{new(map[interface{}]string), "cannot decode to a map with non-string key type"},
 	} {
-		_, err := Decode(`x = 3`, tt.v)
-		if err == nil {
-			t.Errorf("Decode(%v): got nil; want error containing %q",
-				tt.v, tt.want)
-			continue
-		}
-		if !strings.Contains(err.Error(), tt.want) {
-			t.Errorf("Decode(%v): got %q; want error containing %q",
-				tt.v, err, tt.want)
-		}
+		t.Run(fmt.Sprintf("%T", tt.v), func(t *testing.T) {
+			_, err := Decode(`x = 3`, tt.v)
+			if !errorContains(err, tt.want) {
+				t.Errorf("wrong error\nhave: %q\nwant: %q", err, tt.want)
+			}
+		})
 	}
 }
Repository: BurntSushi/toml
Pull request #281 (2021-06-09 15:02:02)

Encode toml.Primitive values 
`toml.Primitive` values didn't get encoded correctly, so ensure they do now.

Follow-up to #63, but with a better/clearer test case.


Patch:
diff --git a/decode.go b/decode.go
index 472c698..c3a62ec 100644
--- a/decode.go
+++ b/decode.go
@@ -152,6 +152,7 @@ func DecodeReader(r io.Reader, v interface{}) (MetaData, error) {
 func (md *MetaData) unify(data interface{}, rv reflect.Value) error {
 
 	// Special case. Look for a `Primitive` value.
+	// TODO: #76 would make this superfluous after implemented.
 	if rv.Type() == reflect.TypeOf((*Primitive)(nil)).Elem() {
 		// Save the undecoded data and the key context into the primitive
 		// value.
diff --git a/encode.go b/encode.go
index fe2e6e2..a5f5803 100644
--- a/encode.go
+++ b/encode.go
@@ -114,10 +114,14 @@ func (enc *Encoder) encode(key Key, rv reflect.Value) {
 	// Special case. If we can marshal the type to text, then we used that.
 	// Basically, this prevents the encoder for handling these types as
 	// generic structs (or whatever the underlying type of a TextMarshaler is).
-	switch rv.Interface().(type) {
+	switch t := rv.Interface().(type) {
 	case time.Time, encoding.TextMarshaler:
 		enc.keyEqElement(key, rv)
 		return
+	// TODO: #76 would make this superfluous after implemented.
+	case Primitive:
+		enc.encode(key, reflect.ValueOf(t.undecoded))
+		return
 	}
 
 	k := rv.Kind()


Test patch:
diff --git a/encode_test.go b/encode_test.go
index 4d91738..def8816 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -556,9 +556,9 @@ func TestEncodeAnonymousStructPointerField(t *testing.T) {
 }
 
 func TestEncodeNestedAnonymousStructs(t *testing.T) {
-	type A struct {	A string }
-	type B struct { B string }
-	type C struct { C string }
+	type A struct{ A string }
+	type B struct{ B string }
+	type C struct{ C string }
 	type BC struct {
 		B
 		C
@@ -595,6 +595,41 @@ func TestEncodeIgnoredFields(t *testing.T) {
 	encodeExpected(t, "ignored field", value, expected, nil)
 }
 
+func TestEncodePrimitive(t *testing.T) {
+	type MyStruct struct {
+		Data  Primitive
+		DataA int
+		DataB string
+	}
+
+	decodeAndEncode := func(toml string) string {
+		var s MyStruct
+		_, err := Decode(toml, &s)
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		var buf bytes.Buffer
+		err = NewEncoder(&buf).Encode(s)
+		if err != nil {
+			t.Fatal(err)
+		}
+		return buf.String()
+	}
+
+	original := `DataA = 1
+DataB = "bbb"
+Data = ["Foo", "Bar"]
+`
+	reEncoded := decodeAndEncode(decodeAndEncode(original))
+
+	if reEncoded != original {
+		t.Errorf(
+			"re-encoded not the same as original\noriginal:   %q\nre-encoded: %q",
+			original, reEncoded)
+	}
+}
+
 func encodeExpected(
 	t *testing.T, label string, val interface{}, wantStr string, wantErr error,
 ) {