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

Minor bug in ch8_ip_address.cpp #1

Open
nexushoratio opened this issue Jun 19, 2023 · 1 comment
Open

Minor bug in ch8_ip_address.cpp #1

nexushoratio opened this issue Jun 19, 2023 · 1 comment

Comments

@nexushoratio
Copy link

Issue Overview

Minor bug parsing IP Addresses in ch8_ip_address.cpp

Describe your environment

The following line incorrectly includes square brackets in the character set:

    21	    if(str.find_first_not_of("[0123456789]") == std::string::npos)

Steps to Reproduce

  1. [1].2.3.4 throws an exception due to stoi("[1]")
  2. 1].2.3.4 gives a false positive due to stoi("1]") succeeding with returning 1.

Expected Behavior

[1].2.3.4 and 1].2.3.4 should both fail gracefully.

Current Behavior

$ ./a.out

Enter an IP address in decimal: [1].2.3.4
 
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi
Aborted
$ ./a.out

Enter an IP address in decimal: 1].2.3.4
 
1].2.3.4 is a valid IP address.

Possible Solution

Remove square brackets, and it works.

$ ./a.out

Enter an IP address in decimal: [1].2.3.4
 
[1].2.3.4 is not a valid IP address.
$ ./a.out

Enter an IP address in decimal: 1].2.3.4
 
1].2.3.4 is not a valid IP address.
@kuashio
Copy link
Collaborator

kuashio commented Jun 19, 2023

Oops, thanks!

jmsgomes added a commit to jmsgomes/cpp-code-challenges-3096958 that referenced this issue Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants