Repository: fxamacker/cbor
Pull request #343 (2022-05-15 17:22:10)

merge master into feature/stream-mode branch
This makes feature/stream-mode branch a superset of master branch again.


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 0747afc..95f1ee6 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1,26 +1,40 @@
-# GitHub Actions - CI for Go to build & test.  See ci-go-cover.yml and safer-golangci-lint.yml for more.
+# GitHub Actions - CI for Go to build & test.
 # https://github.com/fxamacker/cbor/workflows/ci.yml
+# See ci-go-cover.yml for coverage and safer-golangci-lint.yml for linting.
 name: ci
-on: [push]
-jobs:
 
+# Revoke default permissions.
+permissions: {}
+
+on:
+  workflow_dispatch:
+  pull_request:
+  push:
+    branches:
+      - 'master'
+      - 'release*'
+    tags:
+      - 'v*'  
+jobs:
   # Test on various OS with default Go version. 
   tests:
-    name: Test on ${{matrix.os}}
+    name: test ${{matrix.os}} go-${{ matrix.go-version }}
     runs-on: ${{ matrix.os }}
+    permissions:
+      contents: read    
     strategy:
       matrix:
         os: [macos-latest, ubuntu-latest, windows-latest]
-        go-version: [1.15.x, 1.16.x, 1.17.x]
-        
+        go-version: [1.16, 1.17, 1.18]
     steps:
     - name: Install Go
-      uses: actions/setup-go@v2
+      uses: actions/setup-go@v3
       with:
         go-version: ${{ matrix.go-version }}
+        check-latest: true
         
     - name: Checkout code
-      uses: actions/checkout@v2
+      uses: actions/checkout@v3
       with:
         fetch-depth: 1
 
diff --git a/.golangci.yml b/.golangci.yml
index 20de3cb..7b5395c 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -22,9 +22,9 @@ linters-settings:
       - octalLiteral
       - paramTypeCombine
       - whyNoLint
-      - wrapperFunc    
+      - wrapperFunc
   gofmt:
-    simplify: false    
+    simplify: false
   goimports:
     local-prefixes: github.com/fxamacker/cbor
   golint:
@@ -71,7 +71,7 @@ issues:
         - goconst
         - dupl
         - gomnd
-        - lll        
+        - lll
     - path: doc\.go
       linters:
         - goimports
diff --git a/stream.go b/stream.go
index 915f6a6..8ad3f9a 100644
--- a/stream.go
+++ b/stream.go
@@ -200,7 +200,6 @@ func (m *RawMessage) UnmarshalCBOR(data []byte) error {
 	if m == nil {
 		return errors.New("cbor.RawMessage: UnmarshalCBOR on nil pointer")
 	}
-	*m = make([]byte, len(data))
-	copy(*m, data)
+	*m = append((*m)[0:0], data...)
 	return nil
 }


Test patch:
diff --git a/stream_test.go b/stream_test.go
index 6469628..8d4282e 100644
--- a/stream_test.go
+++ b/stream_test.go
@@ -5,6 +5,7 @@ package cbor
 
 import (
 	"bytes"
+	"fmt"
 	"io"
 	"reflect"
 	"testing"
@@ -387,6 +388,20 @@ func TestRawMessage(t *testing.T) {
 	if !bytes.Equal(b, cborData) {
 		t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, cborData)
 	}
+
+	address := fmt.Sprintf("%p", *v.B)
+	if err := Unmarshal(v.A, v.B); err != nil {
+		t.Fatalf("Unmarshal(0x%x) returned error %v", v.A, err)
+	}
+	if address != fmt.Sprintf("%p", *v.B) {
+		t.Fatalf("Unmarshal RawMessage should reuse underlying array if it has sufficient capacity")
+	}
+	if err := Unmarshal(cborData, v.B); err != nil {
+		t.Fatalf("Unmarshal(0x%x) returned error %v", cborData, err)
+	}
+	if address == fmt.Sprintf("%p", *v.B) {
+		t.Fatalf("Unmarshal RawMessage should allocate a new underlying array if it does not have sufficient capacity")
+	}
 }
 
 func TestNullRawMessage(t *testing.T) {