Skip to content

Commit e47a6ab

Browse files
committed
fixed memory read issue in http adapter
1 parent 95e159f commit e47a6ab

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
idea
2-
cmd
1+
.idea
2+
cmd
3+
tests

adapter-http.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ func (h *HttpAdapter) SendRequest(url string, req *Request, additionalResponseDa
4444
return nil, err
4545
}
4646

47-
var body io.Reader
4847
size := len(payload)
49-
48+
var body io.Reader
5049
if req.File != nil && req.FileSize != -1 {
5150
size += req.FileSize
52-
5351
body = io.MultiReader(bytes.NewBuffer(payload), req.File)
5452
} else {
5553
body = bytes.NewBuffer(payload)
@@ -79,13 +77,25 @@ func (h *HttpAdapter) SendRequest(url string, req *Request, additionalResponseDa
7977
}
8078
}
8179

82-
resp, err := NewResponseDecoder(httpResp.Body).Decode(additionalResponseData)
80+
// buffer response to avoid read issues
81+
buf := new(bytes.Buffer)
82+
if httpResp.ContentLength > 0 {
83+
buf.Grow(int(httpResp.ContentLength))
84+
}
85+
if _, err := io.Copy(buf, httpResp.Body); err != nil {
86+
return nil, fmt.Errorf("unable to buffer response: %w", err)
87+
}
88+
89+
ippResp, err := NewResponseDecoder(buf).Decode(additionalResponseData)
8390
if err != nil {
8491
return nil, err
8592
}
8693

87-
err = resp.CheckForErrors()
88-
return resp, err
94+
if err = ippResp.CheckForErrors(); err != nil {
95+
return nil, fmt.Errorf("received error IPP response: %w", err)
96+
}
97+
98+
return ippResp, nil
8999
}
90100

91101
func (h *HttpAdapter) GetHttpUri(namespace string, object interface{}) string {

adapter-socket.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ func NewSocketAdapter(host string, useTLS bool) *SocketAdapter {
4141
}
4242
}
4343

44-
//DoRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred.
45-
//Additional data will be written to an io.Writer if additionalData is not nil
44+
// SendRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred.
45+
// Additional data will be written to an io.Writer if additionalData is not nil
4646
func (h *SocketAdapter) SendRequest(url string, r *Request, additionalData io.Writer) (*Response, error) {
4747
for i := 0; i < h.RequestRetryLimit; i++ {
4848
// encode request
4949
payload, err := r.Encode()
5050
if err != nil {
51-
return nil, fmt.Errorf("unable to encode IPP request: %v", err)
51+
return nil, fmt.Errorf("unable to encode IPP request: %w", err)
5252
}
5353

5454
var body io.Reader
@@ -64,7 +64,7 @@ func (h *SocketAdapter) SendRequest(url string, r *Request, additionalData io.Wr
6464

6565
req, err := http.NewRequest("POST", url, body)
6666
if err != nil {
67-
return nil, fmt.Errorf("unable to create HTTP request: %v", err)
67+
return nil, fmt.Errorf("unable to create HTTP request: %w", err)
6868
}
6969

7070
sock, err := h.GetSocket()
@@ -91,39 +91,42 @@ func (h *SocketAdapter) SendRequest(url string, r *Request, additionalData io.Wr
9191
}
9292

9393
// send request
94-
resp, err := unixClient.Do(req)
94+
httpResp, err := unixClient.Do(req)
9595
if err != nil {
96-
return nil, fmt.Errorf("unable to perform HTTP request: %v", err)
96+
return nil, fmt.Errorf("unable to perform HTTP request: %w", err)
9797
}
9898

99-
if resp.StatusCode == http.StatusUnauthorized {
99+
if httpResp.StatusCode == http.StatusUnauthorized {
100100
// retry with newly generated cert
101-
resp.Body.Close()
101+
httpResp.Body.Close()
102102
continue
103103
}
104104

105-
if resp.StatusCode != http.StatusOK {
106-
resp.Body.Close()
107-
return nil, fmt.Errorf("server did not return Status OK: %d", resp.StatusCode)
105+
if httpResp.StatusCode != http.StatusOK {
106+
httpResp.Body.Close()
107+
return nil, fmt.Errorf("server did not return Status OK: %d", httpResp.StatusCode)
108108
}
109109

110110
// buffer response to avoid read issues
111111
buf := new(bytes.Buffer)
112-
if _, err := io.Copy(buf, resp.Body); err != nil {
113-
resp.Body.Close()
114-
return nil, fmt.Errorf("unable to buffer response: %v", err)
112+
if httpResp.ContentLength > 0 {
113+
buf.Grow(int(httpResp.ContentLength))
114+
}
115+
if _, err := io.Copy(buf, httpResp.Body); err != nil {
116+
httpResp.Body.Close()
117+
return nil, fmt.Errorf("unable to buffer response: %w", err)
115118
}
116119

117-
resp.Body.Close()
120+
httpResp.Body.Close()
118121

119122
// decode reply
120-
ippResp, err := NewResponseDecoder(bytes.NewReader(buf.Bytes())).Decode(additionalData)
123+
ippResp, err := NewResponseDecoder(buf).Decode(additionalData)
121124
if err != nil {
122-
return nil, fmt.Errorf("unable to decode IPP response: %v", err)
125+
return nil, fmt.Errorf("unable to decode IPP response: %w", err)
123126
}
124127

125128
if err = ippResp.CheckForErrors(); err != nil {
126-
return nil, fmt.Errorf("received error IPP response: %v", err)
129+
return nil, fmt.Errorf("received error IPP response: %w", err)
127130
}
128131

129132
return ippResp, nil
@@ -132,7 +135,7 @@ func (h *SocketAdapter) SendRequest(url string, r *Request, additionalData io.Wr
132135
return nil, errors.New("request retry limit exceeded")
133136
}
134137

135-
//GetSocket returns the path to the cupsd socket by searching SocketSearchPaths
138+
// GetSocket returns the path to the cupsd socket by searching SocketSearchPaths
136139
func (h *SocketAdapter) GetSocket() (string, error) {
137140
for _, path := range h.SocketSearchPaths {
138141
fi, err := os.Stat(path)
@@ -142,7 +145,7 @@ func (h *SocketAdapter) GetSocket() (string, error) {
142145
} else if os.IsPermission(err) {
143146
return "", errors.New("unable to access socket: Access denied")
144147
}
145-
return "", fmt.Errorf("unable to access socket: %v", err)
148+
return "", fmt.Errorf("unable to access socket: %w", err)
146149
}
147150

148151
if fi.Mode()&os.ModeSocket != 0 {
@@ -153,7 +156,7 @@ func (h *SocketAdapter) GetSocket() (string, error) {
153156
return "", SocketNotFoundError
154157
}
155158

156-
//GetCert returns the current CUPs authentication certificate by searching CertSearchPaths
159+
// GetCert returns the current CUPs authentication certificate by searching CertSearchPaths
157160
func (h *SocketAdapter) GetCert() (string, error) {
158161
for _, path := range h.CertSearchPaths {
159162
f, err := os.Open(path)
@@ -163,13 +166,13 @@ func (h *SocketAdapter) GetCert() (string, error) {
163166
} else if os.IsPermission(err) {
164167
return "", errors.New("unable to access certificate: Access denied")
165168
}
166-
return "", fmt.Errorf("unable to access certificate: %v", err)
169+
return "", fmt.Errorf("unable to access certificate: %w", err)
167170
}
168171
defer f.Close()
169172

170173
buf := new(bytes.Buffer)
171174
if _, err := io.Copy(buf, f); err != nil {
172-
return "", fmt.Errorf("unable to access certificate: %v", err)
175+
return "", fmt.Errorf("unable to access certificate: %w", err)
173176
}
174177
return buf.String(), nil
175178
}

0 commit comments

Comments
 (0)