Skip to content

Commit 51ccb31

Browse files
Fix buffer overflow issue
* this was for stdin processing of large markdown contents
1 parent 1ae491a commit 51ccb31

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

prompts/pylint/1-generate-violations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ docker run -it --rm vonwig/pylint:latest '{}' man
1919

2020
# prompt user
2121

22-
Run pylint with the arguments `-f json --output /thread/violations.json src/app.py`.
22+
Run pylint with the arguments `-f json --output /thread/violations.json **/*.py`
2323

prompts/pylint/3-4-json-violations->sql-violations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ It should then iterate over each element of an array with the following schema:
2121

2222
```json
2323
[
24-
{"name": "hello", "age": 1}
24+
{"message": "some violation", "path": "app.py", "type": "error"}
2525
]
2626
```
2727

src/docker.clj

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@
235235
(def extract-facts run-function)
236236

237237
(defn write-stdin [container-id content]
238-
(let [buf (ByteBuffer/allocate 1024)
238+
(let [buf (ByteBuffer/allocate (* 2 (count content)))
239239
address (UnixDomainSocketAddress/of "/var/run/docker.sock")
240240
client (SocketChannel/open address)]
241241

@@ -245,11 +245,14 @@
245245
(.clear buf)
246246
(.put buf (.getBytes (String. (format "POST /containers/%s/attach?stdin=true&stream=true HTTP/1.1\n" container-id))))
247247
(.put buf (.getBytes (String. "Host: localhost\nConnection: Upgrade\nUpgrade: tcp\n\n")))
248-
(.put buf (.getBytes content))
249-
(.flip buf)
250-
251-
(while (.hasRemaining buf)
252-
(.write client buf))
248+
(try
249+
(.put buf (.getBytes content))
250+
(.flip buf)
251+
(while (.hasRemaining buf)
252+
(.write client buf))
253+
(catch Throwable t
254+
(println "could not write to stdin " t)
255+
client))
253256
client))
254257

255258
(defn docker-stream-format->stdout [bytes]
@@ -261,7 +264,11 @@
261264

262265
(catch Throwable t
263266
(println t)))
264-
(String. (Arrays/copyOfRange bytes 8 (count bytes))))
267+
(try
268+
(String. (Arrays/copyOfRange bytes 8 (count bytes)))
269+
(catch Throwable t
270+
(println t)
271+
"")))
265272

266273
(defn function-call-with-stdin [m]
267274
(-pull m)
@@ -276,18 +283,36 @@
276283

277284
(defn finish-call
278285
"This is a blocking call that waits for the container to finish and then returns the output and exit code."
279-
[x]
286+
[{:keys [timeout] :or {timeout 10000} :as x}]
287+
;; close stdin socket
280288
(.close (:socket x))
281-
(wait x)
282-
;; body is raw PTY output
283-
(let [s (docker-stream-format->stdout
284-
(:body
285-
(attach-container-stdout-logs x)))
286-
info (inspect x)]
287-
(delete x)
288-
{:pty-output s
289-
:exit-code (-> info :State :ExitCode)
290-
:info info}))
289+
;; timeout process
290+
(let [finished-channel (async/promise-chan)]
291+
(async/go
292+
(async/<! (async/timeout timeout))
293+
(async/>! finished-channel {:timeout timeout
294+
:done :timeout
295+
:kill-container (kill-container x)}))
296+
;; watch the container
297+
(async/go
298+
(wait x)
299+
(async/>! finished-channel {:done :exited}))
300+
;; body is raw PTY output
301+
(try
302+
(let [finish-reason (async/<!! finished-channel)
303+
s (docker-stream-format->stdout
304+
(:body
305+
(attach-container-stdout-logs x)))
306+
info (inspect x)]
307+
(delete x)
308+
(merge
309+
finish-reason
310+
{:pty-output s
311+
:exit-code (-> info :State :ExitCode)
312+
:info info}))
313+
(catch Throwable t
314+
(delete x)
315+
{}))))
291316

292317
(comment
293318

src/markdown.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@
6464
x (docker/function-call-with-stdin
6565
{:image "docker/lsp:treesitter"
6666
:content content})
67-
{s :pty-output} (async/<! (async/thread
68-
(Thread/sleep 10)
69-
(docker/finish-call x)))]
67+
{s :pty-output} (async/<!! (async/thread
68+
(Thread/sleep 10)
69+
(docker/finish-call x)))]
7070
(->> s
7171
(edn/read-string)
7272
(extract-prompts content)
@@ -78,6 +78,8 @@
7878
(def content (slurp "prompts/qrencode/README.md"))
7979
(pprint (parse-markdown content))
8080

81+
(parse-markdown (slurp "prompts/pylint/docs.md"))
82+
8183
(def t
8284
'("section"
8385
"4:1-8:1"

0 commit comments

Comments
 (0)