nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: qmuntal/gltf
Pull request #47 (2022-01-10 17:57:29)

Validate accessor type


Patch:
diff --git a/const.go b/const.go
index dd7c350..a5c049d 100644
--- a/const.go
+++ b/const.go
@@ -1,6 +1,9 @@
 package gltf
 
-import "encoding/json"
+import (
+	"encoding/json"
+	"fmt"
+)
 
 var (
 	// DefaultMatrix defines an identity matrix.
@@ -129,7 +132,7 @@ func (a *AccessorType) UnmarshalJSON(data []byte) error {
 	var tmp string
 	err := json.Unmarshal(data, &tmp)
 	if err == nil {
-		*a = map[string]AccessorType{
+		accType, ok := map[string]AccessorType{
 			"SCALAR": AccessorScalar,
 			"VEC2":   AccessorVec2,
 			"VEC3":   AccessorVec3,
@@ -138,6 +141,10 @@ func (a *AccessorType) UnmarshalJSON(data []byte) error {
 			"MAT3":   AccessorMat3,
 			"MAT4":   AccessorMat4,
 		}[tmp]
+		if !ok {
+			return fmt.Errorf("gltf: unknown accessor's type: %s", tmp)
+		}
+		*a = accType
 	}
 	return err
 }


Test patch:
diff --git a/const_test.go b/const_test.go
new file mode 100644
index 0000000..4bfc349
--- /dev/null
+++ b/const_test.go
@@ -0,0 +1,34 @@
+package gltf
+
+import (
+	"encoding/json"
+	"testing"
+)
+
+func TestAccessorType_UnmarshalJSON(t *testing.T) {
+	type args struct {
+		defaultType AccessorType
+		expType     AccessorType
+		typeStr     []byte
+	}
+	tests := []struct {
+		name    string
+		args    args
+		wantErr bool
+	}{
+		{"base", args{50, AccessorVec3, []byte(`"VEC3"`)}, false},
+		{"incorrect-type", args{100, 100, []byte(`"CUSTOM_TYPE"`)}, true},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			err := json.Unmarshal(tt.args.typeStr, &tt.args.defaultType)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
+				return
+			}
+			if tt.args.defaultType != tt.args.expType {
+				t.Errorf("Expected: %d, got: %d", tt.args.expType, tt.args.defaultType)
+			}
+		})
+	}
+}
Repository: qmuntal/gltf
Pull request #57 (2022-07-09 23:25:29)

Added SetJSONIndent to Encoder
This pretty prints encoded gltf, e.g.
```json
{"buffers":[{"uri":"data:application/octet-stream;base64
```

turns to
```json
{
	"buffers": [
		{
			"uri": "data:application/octet-stream;base64
```
just by setting e.g.

```go
enc := gltf.NewEncoder(w)
enc.AsBinary = false
enc.SetIndent("", "\t") // <-- this line
err = enc.Encode(doc)
```


Patch:
diff --git a/encode.go b/encode.go
index a5a58d5..1f56239 100644
--- a/encode.go
+++ b/encode.go
@@ -60,6 +60,8 @@ type Encoder struct {
 	AsBinary bool
 	Fsys     CreateFS
 	w        io.Writer
+	indent   string
+	prefix   string
 }
 
 // NewEncoder returns a new encoder that writes to w as a normal glTF file.
@@ -79,6 +81,12 @@ func NewEncoderFS(w io.Writer, fsys CreateFS) *Encoder {
 	}
 }
 
+// SetJSONIndent sets json encoded data to have provided prefix and indent settings
+func (e *Encoder) SetJSONIndent(prefix string, indent string) {
+	e.prefix = prefix
+	e.indent = indent
+}
+
 // Encode writes the encoding of doc to the stream.
 func (e *Encoder) Encode(doc *Document) error {
 	var err error
@@ -217,6 +225,9 @@ func (e *Encoder) marshalJSONDoc(doc *Document) ([]byte, error) {
 			tmp.CustomBuffers[i] = buf
 		}
 	}
+	if len(e.prefix) > 0 || len(e.indent) > 0 {
+		return json.MarshalIndent(tmp, e.prefix, e.indent)
+	}
 	return json.Marshal(tmp)
 }
 


Test patch:
diff --git a/encode_test.go b/encode_test.go
index 5116cd5..725d9ba 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -48,6 +48,19 @@ func saveMemory(doc *Document, asBinary bool) (*Decoder, error) {
 	return NewDecoderFS(buff, m), nil
 }
 
+func saveMemoryIndent(doc *Document, asBinary bool) (*Decoder, error) {
+	buff := new(bytes.Buffer)
+	m := mockChunkReadHandler{fstest.MapFS{}}
+	e := NewEncoderFS(buff, m)
+	e.AsBinary = asBinary
+	e.SetJSONIndent("", "\t")
+	if err := e.Encode(doc); err != nil {
+		return nil, err
+	}
+
+	return NewDecoderFS(buff, m), nil
+}
+
 func TestEncoder_Encode_AsBinary_WithoutBuffer(t *testing.T) {
 	doc := &Document{}
 	buff := new(bytes.Buffer)
@@ -243,6 +256,20 @@ func TestEncoder_Encode(t *testing.T) {
 						return
 					}
 				}
+
+				d, err = saveMemoryIndent(tt.args.doc, method == "binary")
+				if (err != nil) != tt.wantErr {
+					t.Errorf("Encoder.Encode() withIndent error = %v, wantErr %v", err, tt.wantErr)
+					return
+				}
+				if !tt.wantErr {
+					doc := new(Document)
+					d.Decode(doc)
+					if diff := deep.Equal(doc, tt.args.doc); diff != nil {
+						t.Errorf("Encoder.Encode() withIndent = %v", diff)
+						return
+					}
+				}
 			})
 		}
 	}
Repository: qmuntal/gltf
Pull request #69 (2023-07-05 09:29:11)

Avoid out of bounds when calculating b.URI[startPos:]
Processing illegal base64 content may cause the program to panic, For example, the following gltf file:

```
{"buffers": [
    {
      "uri": "data:application/octet-stream;base64",
      "byteLength": 1
    }
  ]
}
```

When our program calculates the index, the split character is counted by default, and 1 is automatically added, resulting in an out-of-bounds problem in the program.

```
func (b *Buffer) marshalData() ([]byte, error) {
	if !b.IsEmbeddedResource() {
		return nil, nil
	}
	startPos := len(mimetypeApplicationOctet) + 1//startPos is 37
	//But our b.URI is only 36 in length in this processing.
	sl, err := base64.StdEncoding.DecodeString(b.URI[startPos:])
	if len(sl) == 0 || err != nil {
		return nil, err
	}
	return sl, nil
}
```

## what happened

```
❯ go run main.go
panic: runtime error: slice bounds out of range [37:36]

goroutine 1 [running]:
github.com/qmuntal/gltf.(*Buffer).marshalData(0x1400012c1e0)
        /Users/housihan/go/pkg/mod/github.com/qmuntal/gltf@v0.24.0/gltf.go:136 +0xdc
```


Patch:
diff --git a/gltf.go b/gltf.go
index c833cd1..182a269 100644
--- a/gltf.go
+++ b/gltf.go
@@ -2,6 +2,7 @@ package gltf
 
 import (
 	"encoding/base64"
+	"errors"
 	"strings"
 	"sync"
 )
@@ -133,6 +134,9 @@ func (b *Buffer) marshalData() ([]byte, error) {
 		return nil, nil
 	}
 	startPos := len(mimetypeApplicationOctet) + 1
+	if len(b.URI) < startPos {
+		return nil, errors.New("gltf: Invalid base64 content")
+	}
 	sl, err := base64.StdEncoding.DecodeString(b.URI[startPos:])
 	if len(sl) == 0 || err != nil {
 		return nil, err


Test patch:
diff --git a/gltf_test.go b/gltf_test.go
index c33a359..eea3ad5 100644
--- a/gltf_test.go
+++ b/gltf_test.go
@@ -96,6 +96,7 @@ func TestBuffer_marshalData(t *testing.T) {
 		{"empty", &Buffer{URI: "data:application/octet-stream;base64,"}, nil, false},
 		{"test", &Buffer{URI: "data:application/octet-stream;base64,TEST"}, []byte{76, 68, 147}, false},
 		{"complex", &Buffer{URI: "data:application/octet-stream;base64,YW55IGNhcm5hbCBwbGVhcw=="}, []byte{97, 110, 121, 32, 99, 97, 114, 110, 97, 108, 32, 112, 108, 101, 97, 115}, false},
+		{"invalid", &Buffer{URI: "data:application/octet-stream;base64"}, nil, true},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
Repository: qmuntal/gltf
Pull request #70 (2023-07-22 15:45:55)

Avoid out of bounds when calculating im.URI[startPos:]
Hello, when I was looking at pull 69, I found that there were similar problems when processing images. I fixed it and added test cases. Also edited one of your details. Please take a look~


Patch:
diff --git a/gltf.go b/gltf.go
index 182a269..e9d416f 100644
--- a/gltf.go
+++ b/gltf.go
@@ -415,6 +415,9 @@ func (im *Image) MarshalData() ([]byte, error) {
 		mimetype = mimetypeImageJPG
 	}
 	startPos := len(mimetype) + 1
+	if len(im.URI) < startPos {
+		return []byte{}, errors.New("gltf: Invalid base64 content")
+	}
 	return base64.StdEncoding.DecodeString(im.URI[startPos:])
 }
 


Test patch:
diff --git a/encode_test.go b/encode_test.go
index 112fb30..e43dcbf 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -288,6 +288,7 @@ func TestImage_MarshalData(t *testing.T) {
 		{"empty", &Image{URI: "data:image/jpeg;base64,"}, []byte{}, false},
 		{"test", &Image{URI: "data:image/png;base64,TEST"}, []byte{76, 68, 147}, false},
 		{"complex", &Image{URI: "data:image/png;base64,YW55IGNhcm5hbCBwbGVhcw=="}, []byte{97, 110, 121, 32, 99, 97, 114, 110, 97, 108, 32, 112, 108, 101, 97, 115}, false},
+		{"invalid", &Image{URI: "data:image/png;base64"}, []byte{}, true},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
diff --git a/gltf_test.go b/gltf_test.go
index eea3ad5..0611f16 100644
--- a/gltf_test.go
+++ b/gltf_test.go
@@ -72,7 +72,7 @@ func TestImage_IsEmbeddedResource(t *testing.T) {
 		want bool
 	}{
 		{"png", &Image{URI: "data:image/png;base64,dsjdsaGGUDXGA"}, true},
-		{"jpg", &Image{URI: "data:image/png;base64,dsjdsaGGUDXGA"}, true},
+		{"jpg", &Image{URI: "data:image/jpeg;base64,dsjdsaGGUDXGA"}, true},
 		{"external", &Image{URI: "https://web.com/a"}, false},
 	}
 	for _, tt := range tests {