Skip to content

Commit 6d7c021

Browse files
committed
use strings.NewReader
1 parent cdb23cf commit 6d7c021

File tree

13 files changed

+18
-19
lines changed

13 files changed

+18
-19
lines changed

models/asymkey/ssh_key_commit_verification.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package asymkey
55

66
import (
7-
"bytes"
87
"context"
98
"fmt"
109
"strings"
@@ -65,7 +64,7 @@ func ParseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committer *
6564
}
6665

6766
func verifySSHCommitVerification(sig, payload string, k *PublicKey, committer, signer *user_model.User, email string) *CommitVerification {
68-
if err := sshsig.Verify(bytes.NewBufferString(payload), []byte(sig), []byte(k.Content), "git"); err != nil {
67+
if err := sshsig.Verify(strings.NewReader(payload), []byte(sig), []byte(k.Content), "git"); err != nil {
6968
return nil
7069
}
7170

models/asymkey/ssh_key_verify.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
package asymkey
55

66
import (
7-
"bytes"
87
"context"
8+
"strings"
99

1010
"code.gitea.io/gitea/models/db"
1111
"code.gitea.io/gitea/modules/log"
@@ -30,11 +30,11 @@ func VerifySSHKey(ctx context.Context, ownerID int64, fingerprint, token, signat
3030
return "", ErrKeyNotExist{}
3131
}
3232

33-
err = sshsig.Verify(bytes.NewBufferString(token), []byte(signature), []byte(key.Content), "gitea")
33+
err = sshsig.Verify(strings.NewReader(token), []byte(signature), []byte(key.Content), "gitea")
3434
if err != nil {
3535
// edge case for Windows based shells that will add CR LF if piped to ssh-keygen command
3636
// see https://github.com/PowerShell/PowerShell/issues/5974
37-
if sshsig.Verify(bytes.NewBufferString(token+"\r\n"), []byte(signature), []byte(key.Content), "gitea") != nil {
37+
if sshsig.Verify(strings.NewReader(token+"\r\n"), []byte(signature), []byte(key.Content), "gitea") != nil {
3838
log.Error("Unable to validate token signature. Error: %v", err)
3939
return "", ErrSSHInvalidTokenSignature{
4040
Fingerprint: key.Fingerprint,

modules/httplib/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (r *Request) Body(data any) *Request {
108108
switch t := data.(type) {
109109
case nil: // do nothing
110110
case string:
111-
bf := bytes.NewBufferString(t)
111+
bf := strings.NewReader(t)
112112
r.req.Body = io.NopCloser(bf)
113113
r.req.ContentLength = int64(len(t))
114114
case []byte:

modules/lfs/http_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (a *DummyTransferAdapter) Name() string {
3131
}
3232

3333
func (a *DummyTransferAdapter) Download(ctx context.Context, l *Link) (io.ReadCloser, error) {
34-
return io.NopCloser(bytes.NewBufferString("dummy")), nil
34+
return io.NopCloser(strings.NewReader("dummy")), nil
3535
}
3636

3737
func (a *DummyTransferAdapter) Upload(ctx context.Context, l *Link, p Pointer, r io.Reader) error {
@@ -49,7 +49,7 @@ func lfsTestRoundtripHandler(req *http.Request) *http.Response {
4949
if strings.Contains(url, "status-not-ok") {
5050
return &http.Response{StatusCode: http.StatusBadRequest}
5151
} else if strings.Contains(url, "invalid-json-response") {
52-
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewBufferString("invalid json"))}
52+
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("invalid json"))}
5353
} else if strings.Contains(url, "valid-batch-request-download") {
5454
batchResponse = &BatchResponse{
5555
Transfer: "dummy",

modules/lfs/transferadapter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestBasicTransferAdapter(t *testing.T) {
3232
if strings.Contains(url, "download-request") {
3333
assert.Equal(t, "GET", req.Method)
3434

35-
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewBufferString("dummy"))}
35+
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("dummy"))}
3636
} else if strings.Contains(url, "upload-request") {
3737
assert.Equal(t, "PUT", req.Method)
3838
assert.Equal(t, "application/octet-stream", req.Header.Get("Content-Type"))
@@ -126,7 +126,7 @@ func TestBasicTransferAdapter(t *testing.T) {
126126
}
127127

128128
for n, c := range cases {
129-
err := a.Upload(t.Context(), c.link, p, bytes.NewBufferString("dummy"))
129+
err := a.Upload(t.Context(), c.link, p, strings.NewReader("dummy"))
130130
if len(c.expectederror) > 0 {
131131
assert.Contains(t, err.Error(), c.expectederror, "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror)
132132
} else {

modules/storage/azureblob_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func Test_azureBlobObject(t *testing.T) {
7676
assert.NoError(t, err)
7777

7878
data := "Q2xTckt6Y1hDOWh0"
79-
_, err = s.Save("test.txt", bytes.NewBufferString(data), int64(len(data)))
79+
_, err = s.Save("test.txt", strings.NewReader(data), int64(len(data)))
8080
assert.NoError(t, err)
8181
obj, err := s.Open("test.txt")
8282
assert.NoError(t, err)

modules/storage/storage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func testStorageIterator(t *testing.T, typStr Type, cfg *setting.Storage) {
2626
{"b/x 4.txt", "bx4"},
2727
}
2828
for _, f := range testFiles {
29-
_, err = l.Save(f[0], bytes.NewBufferString(f[1]), -1)
29+
_, err = l.Save(f[0], strings.NewReader(f[1]), -1)
3030
assert.NoError(t, err)
3131
}
3232

modules/web/router_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestPathProcessor(t *testing.T) {
5151
}
5252

5353
func TestRouter(t *testing.T) {
54-
buff := bytes.NewBufferString("")
54+
buff := strings.NewReader("")
5555
recorder := httptest.NewRecorder()
5656
recorder.Body = buff
5757

tests/integration/api_repo_branch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestAPIRepoBranchesPlain(t *testing.T) {
5959

6060
req := NewRequest(t, "POST", link.String()).AddTokenAuth(token)
6161
req.Header.Add("Content-Type", "application/json")
62-
req.Body = io.NopCloser(bytes.NewBufferString(`{"new_branch_name":"test_branch2", "old_branch_name": "test_branch", "old_ref_name":"refs/heads/test_branch"}`))
62+
req.Body = io.NopCloser(strings.NewReader(`{"new_branch_name":"test_branch2", "old_branch_name": "test_branch", "old_ref_name":"refs/heads/test_branch"}`))
6363
resp = MakeRequest(t, req, http.StatusCreated)
6464
bs, err = io.ReadAll(resp.Body)
6565
assert.NoError(t, err)
@@ -117,7 +117,7 @@ func TestAPIRepoBranchesMirror(t *testing.T) {
117117

118118
req := NewRequest(t, "POST", link.String()).AddTokenAuth(token)
119119
req.Header.Add("Content-Type", "application/json")
120-
req.Body = io.NopCloser(bytes.NewBufferString(`{"new_branch_name":"test_branch2", "old_branch_name": "test_branch", "old_ref_name":"refs/heads/test_branch"}`))
120+
req.Body = io.NopCloser(strings.NewReader(`{"new_branch_name":"test_branch2", "old_branch_name": "test_branch", "old_ref_name":"refs/heads/test_branch"}`))
121121
resp = MakeRequest(t, req, http.StatusForbidden)
122122
bs, err = io.ReadAll(resp.Body)
123123
assert.NoError(t, err)

tests/integration/editor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func TestWebGitCommitEmail(t *testing.T) {
227227
body := &bytes.Buffer{}
228228
uploadForm := multipart.NewWriter(body)
229229
file, _ := uploadForm.CreateFormFile("file", name)
230-
_, _ = io.Copy(file, bytes.NewBufferString(content))
230+
_, _ = io.Copy(file, strings.NewReader(content))
231231
_ = uploadForm.WriteField("_csrf", GetUserCSRFToken(t, session))
232232
_ = uploadForm.Close()
233233

0 commit comments

Comments
 (0)