forked from modular/mojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[External] [stdlib] Fix
input()
segfaults on EOF (#53925)
[External] [stdlib] Fix `input()` segfaults on EOF pressing `ctrl-d` with no input when `input()` is called causes mojo to crash because `read_until_delimiter()` doesn't check the return value of the C function `getdelim()`. it assumes `getdelim()` always succeeds and so, in the case of an error, it blindly creates a `StringRef` with its length set to the return value - 1 (so the length is -2 in this case). this `StringRef` is then passed to `String()` which in turn passes the `StringRef` to `memcpy()` with a count of -2 and ultimately crashing mojo. this pr adds a check in `read_until_delimiter()` to check if `getdelim()` failed and raise an error if it does, along with a test to ensure `read_until_delimiter()` continues to behave as it should. Fixes modular#3908 Co-authored-by: mahiro21h <raysanalawami@gmail.com> Closes modular#3919 MODULAR_ORIG_COMMIT_REV_ID: c3457f3377bfcfe0379e31fbd31e72ec53fe7516 Signed-off-by: Joshua James Venter <venter.joshua@gmail.com>
- Loading branch information
Showing
2 changed files
with
38 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2024, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
# RUN: echo -n | %mojo %s | ||
|
||
from builtin.io import _fdopen | ||
from testing import testing | ||
|
||
|
||
fn test_read_until_delimiter_raises_eof() raises: | ||
var stdin = _fdopen["r"](0) | ||
with testing.assert_raises(contains="EOF"): | ||
# Assign to a variable to silence a warning about unused String value | ||
# if an error wasn't raised. | ||
var unused = stdin.read_until_delimiter("\n") | ||
|
||
|
||
fn main() raises: | ||
test_read_until_delimiter_raises_eof() |