Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harden curl output parsing #5984

Merged
merged 3 commits into from
Jun 7, 2024
Merged

Conversation

dra27
Copy link
Member

@dra27 dra27 commented Jun 2, 2024

Issue originally reported on Discuss.

The OP was using a native Windows curl.exe from Git-for-Windows git-bash (MSYS2 has curl both for its "Cygwin" main install, but also compiled natively - git-bash installs the mingw64 native-compiled version). This yields an exception during opam init while attempting to setup the internal Cygwin installation:

Fatal error:
OpamDownload.Download_fail(_, "curl: code  while downloading https://cygwin.com/sha512.sum")

Note that there is no code. Running the command manually, everything appears to be OK, but if the output is piped to a file, the issue becomes apparent (the various \r effects removed):

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   291  100   291    0     0    635      0 --:--:-- --:--:-- --:--:--   638200

There is a blank line at the end of the file and the 200 is at the end of the penultimate line. There appears to be an issue with interleaving stderr and stdout when the output is being piped. At this point I could wave my hands and spout Posix, line-buffering and all sorts of explanations, but this PR is a somewhat simpler approach - the status code now has a newline printed before and after it and the parser attempts to convert the first non-blank line from the end of the output to an integer.

I was tempted to add --no-progress-meter, but it turns out that's too recent an option. I'm wondering why --silent has never been added (same for the others), but perhaps that's because if something goes wrong, the display might be useful. We could add --silent --show-error which would add errors only to the log files (--silent is there since the dawn of time, and --show-error was added in 5.9 on 22-May-1999).

@dra27 dra27 added this to the 2.2.0~beta3 milestone Jun 2, 2024
@dra27 dra27 added this to For beta3 in Opam 2.2.0 Jun 2, 2024
@dra27 dra27 force-pushed the harden-curl-parsing branch 2 times, most recently from 9728872 to 53ff088 Compare June 2, 2024 13:54
Comment on lines 116 to 121
let num = try int_of_string code with Failure _ -> 999 in
if num >= 400 then
fail (Some ("curl error code " ^ code),
Printf.sprintf "curl: code %s while downloading %s"
code (OpamUrl.to_string url))
else Done ()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let num = try int_of_string code with Failure _ -> 999 in
if num >= 400 then
fail (Some ("curl error code " ^ code),
Printf.sprintf "curl: code %s while downloading %s"
code (OpamUrl.to_string url))
else Done ()
try
if int_of_string code >= 400 then
fail (Some ("curl error code " ^ code),
Printf.sprintf "curl: code %s while downloading %s"
code (OpamUrl.to_string url))
else Done ()
with Failure _ ->
fail (Some "No error code",
Printf.sprintf "curl: no error code while downloading %s"
(OpamUrl.to_string url))

Like that we give the 'no code' information instead of an unknown 999 code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah - I debated whether the 999 thing should remain as well, so the fact you also thought it suggests it really ought to go!

I pushed an extra commit, but switching it to have fewer try blocks (not necessarily elegantly, though). It also removes the separation of "no output" from "no code parsed", which I think is also fine, as we don't really care about the distinction.

@rjbou rjbou self-requested a review June 3, 2024 16:28
@kit-ty-kate kit-ty-kate self-assigned this Jun 4, 2024
@kit-ty-kate kit-ty-kate force-pushed the harden-curl-parsing branch 2 times, most recently from 701b8f4 to fd1842b Compare June 4, 2024 19:55
@kit-ty-kate kit-ty-kate removed their assignment Jun 4, 2024
@kit-ty-kate kit-ty-kate requested a review from rjbou June 7, 2024 14:05
Copy link
Collaborator

@rjbou rjbou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kit-ty-kate
Copy link
Member

Thanks!

@kit-ty-kate kit-ty-kate merged commit fcf46f3 into ocaml:master Jun 7, 2024
29 checks passed
Opam 2.2.0 automation moved this from For beta3 to Done Jun 7, 2024
@dra27 dra27 mentioned this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Opam 2.2.0
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants