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

[SR-8700] String() function doesn't catch negative number as input for count #51213

Closed
swift-ci opened this issue Sep 5, 2018 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Sep 5, 2018

Previous ID SR-8700
Radar None
Original Reporter luro02 (JIRA User)
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Environment

Ubuntu 18.04 Gnome

Swift 4.2-CONVERGENCE (https://swift.org/builds/swift-4.2-convergence/ubuntu1804/swift-4.2-CONVERGENCE/swift-4.2-CONVERGENCE-ubuntu18.04.tar.gz)

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: c8dddacabc07b6e47b65787cb970e603

Issue Description:

If you try to run the following code sample it crashes with an Illegal instruction,

I am new to swift (started some hours ago....), so I am not sure if this is a bug?! apologies in advance for the inconvenience 🙂

String(
  repeating: ".",
  count: -1
)
crash.swift:1:1: warning: result of 'String' initializer is unused
String(
^     ~
Precondition failed: Negative count not allowed: file /home/buildnode/jenkins/workspace/oss-swift-4.2-package-linux-ubuntu-18_04/swift/stdlib/public/core/StringLegacy.swift, line 51
Current stack trace:
0    libswiftCore.so                    0x00007f8992be6ac0 _swift_stdlib_reportFatalErrorInFile + 215
1    libswiftCore.so                    0x00007f899293f0f1 <unavailable> + 1503473
2    libswiftCore.so                    0x00007f8992b7bc52 <unavailable> + 3849298
3    libswiftCore.so                    0x00007f8992b7df49 <unavailable> + 3858249
4    libswiftCore.so                    0x00007f899293e83a <unavailable> + 1501242
5    libswiftCore.so                    0x00007f8992b7bb98 <unavailable> + 3849112
6    libswiftCore.so                    0x00007f899293e83a <unavailable> + 1501242
7    libswiftCore.so                    0x00007f8992ad8578 <unavailable> + 3179896
8    libswiftCore.so                    0x00007f8992a86eda <unavailable> + 2846426
10   swift                              0x0000000000fe9699 <unavailable> + 12490393
11   swift                              0x0000000000fed610 <unavailable> + 12506640
12   swift                              0x00000000004cd443 <unavailable> + 840771
13   swift                              0x00000000004b408a <unavailable> + 737418
14   swift                              0x00000000004afdc0 <unavailable> + 720320
15   swift                              0x000000000046ae38 <unavailable> + 437816
16   libc.so.6                          0x00007f8994b66ab0 __libc_start_main + 231
17   swift                              0x000000000046904a <unavailable> + 430154
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0x3dee874]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0x3dec68c]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0x3deea32]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x12890)[0x7f8996725890]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/lib/swift/linux/libswiftCore.so(+0x308580)[0x7f8992ad8580]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/lib/swift/linux/libswiftCore.so(+0x2b6eda)[0x7f8992a86eda]
[0x7f8996b37041]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0xfe9699]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0xfed610]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0x4cd443]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0x4b408a]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0x4afdc0]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0x46ae38]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f8994b66b97]
/home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift[0x46904a]
Stack dump:
0.  Program arguments: /home/lucas/.swiftenv/versions/4.2-CONVERGENCE/usr/bin/swift -frontend -interpret crash.swift -disable-objc-interop -color-diagnostics -module-name crash 
Illegal instruction (core dumped)

I think this problem can be fixed if the String function takes an UInt type instead of Int

@belkadan
Copy link
Contributor

belkadan commented Sep 5, 2018

This is the String initializer catching the negative count: it's telling you in the error message "Negative count not allowed".

@belkadan
Copy link
Contributor

belkadan commented Sep 5, 2018

Context: Since Swift doesn't have implicit conversions between the Int and UInt types, the general recommendation is to use Int unless interfacing with an API that really is using "pointer-sized unsigned integers", even though it does mean this sort of error isn't caught at compile time.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants