Avoid crash when `curl_version_info` returns a `NULL` `cainfo` (fix #9189) (#9191)
commit9078e435af2c8ab8261e6bbffecc4ffe19eef87c
authorBo Yang <atry@fb.com>
Wed, 14 Sep 2022 19:01:10 +0000 (14 12:01 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Wed, 14 Sep 2022 19:01:10 +0000 (14 12:01 -0700)
tree0b43ae5463a792aa248e763b13a825ccedeac81d
parent27fc1f358d164a38743b41ca42b447f888e2d6b5
Avoid crash when `curl_version_info` returns a `NULL` `cainfo` (fix #9189) (#9191)

Summary:
According to the document of `curl_version_info` at https://curl.se/libcurl/c/curl_version_info.html, `curl_version_info_data::cainfo` can be `NULL`. It resulted in a segmentation fault at https://github.com/facebook/hhvm/blob/58a3d73cbca795988739c94c10a016aa895f22cf/hphp/runtime/ext/curl/curl-resource.cpp#L1456, when trying to look up a Folly map by an empty local variable `cainfo` of `HPHP::String`, converted from the `NULL` `curl_version_info_data::cainfo`.

This PR avoids the crash by not caching the CA when the local variable `cainfo`  is an empty `HPHP::String`.

Pull Request resolved: https://github.com/facebook/hhvm/pull/9191

Test Plan:
 ---

### Without this PR
Build HHVM from a GitHub Codespace without this PR, and run the following command
```
HHVM_BIN=/workspaces/hhvm/build/hphp/hhvm/hhvm /workspaces/hhvm/build/hphp/hhvm/hhvm hphp/test/run.php hphp/test/slow/ext_curl/curl_close_in_callback.php
```

The test will fail because of segmentation fault.

```
Running 1 tests in 1 threads (0 in serial)

FAILED: hphp/test/slow/ext_curl/curl_close_in_callback.php
001+ Core dumped: Segmentation fault
001- Warning\: curl_close\(\)\: Attempt to close cURL in callback, ignored\. in [^\r\n]+\/curl_close_in_callback\.php on line 7
002+ Stack trace in /tmp/stacktrace.30236.log
003+ /usr/bin/timeout: the monitored command dumped core

1 tests failed
(╯°□°)╯︵ ┻━┻

Run hphp/test/slow/ext_curl/curl_close_in_callback.php by hand:
./build/hphp/hhvm/hhvm -c /workspaces/hhvm/hphp/test/slow/config.ini -vEval.EnableArgsInBacktraces=false -vEval.EnableIntrinsicsExtension=true -vEval.HHIRInliningIgnoreHints=false -vEval.HHIRAlwaysInterpIgnoreHint=false -vEval.FoldLazyClassKeys=false -vEval.EnableLogBridge=false -vUnitFileCache.Path=/tmp/hphp-test-4Sj6I4/unit-cache.sql -vEval.Jit=true  -vEval.ProfileHWEnable=false -vEval.EmbeddedDataExtractPath='/workspaces/hhvm/build/hphp/hhvm/hhvm_%{type}_%{buildid}' -vEval.JitWorkerThreads=1 -vEval.JitWorkerThreadsForSerdes=1  -vAutoload.DB.Path='/tmp/hphp-test-4Sj6I4/hphp/test/slow/ext_curl/curl_close_in_callback.php.autoloadDB.0' -vResourceLimit.CoreFileSize=0     --file 'hphp/test/slow/ext_curl/curl_close_in_callback.php'

See failed test output and expectations:
cat /tmp/hphp-test-4Sj6I4/hphp/test/slow/ext_curl/curl_close_in_callback.php.diff
cat /tmp/hphp-test-4Sj6I4/hphp/test/slow/ext_curl/curl_close_in_callback.php.out
cat hphp/test/slow/ext_curl/curl_close_in_callback.php.expectf

For xargs, list of failures is available using:
cat /tmp/hphp-test-4Sj6I4/test-failures

Re-run just the failing tests:
hphp/test/run  $(cat /tmp/hphp-test-4Sj6I4/test-failures)

Total time for all executed tests as run: 1.21s
Total time for all executed tests if run serially: 1.15s
```
### With this PR

Then build HHVM from a GitHub Codespace with this PR, and run the following command

```
HHVM_BIN=/workspaces/hhvm/build/hphp/hhvm/hhvm /workspaces/hhvm/build/hphp/hhvm/hhvm hphp/test/run.php hphp/test/slow/ext_curl/curl_close_in_callback.php
```

```
Running 1 tests in 1 threads (0 in serial)

All tests passed.
              |    |    |
             )_)  )_)  )_)
            )___))___))___)\
           )____)____)_____)\
         _____|____|____|____\\__
---------\      SHIP IT      /---------
  ^^^^^ ^^^^^^^^^^^^^^^^^^^^^
    ^^^^      ^^^^     ^^^    ^^
         ^^^^      ^^^

Total time for all executed tests as run: 1.15s
Total time for all executed tests if run serially: 1.10s
```

Reviewed By: alexeyt

Differential Revision: D39493211

Pulled By: Atry

fbshipit-source-id: 035688dffe1b7b16249170fbb95b20965c3d2283
hphp/runtime/ext/curl/curl-resource.cpp