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

Exception with "N0" format string and integer value of 0 #118

Closed
nfbot opened this issue Aug 30, 2017 · 3 comments
Closed

Exception with "N0" format string and integer value of 0 #118

nfbot opened this issue Aug 30, 2017 · 3 comments

Comments

@nfbot
Copy link
Member

nfbot commented Aug 30, 2017

From nf-interpreter created by MikroBusNet : nanoframework/nf-interpreter#285

The following code will throw a 'System.ArgumentOutOfRangeException' in mscorlib.dll :

private static uint myVar = 0;

public static void Main()
{
    Debug.Print("myVar = " + myVar.ToString("N0"));
}

If myVar is 0 and precision is 0 for the 'N' format string, then the FormatNative() method is returning an empty string, which will raise the exception in the InsertGroupSeparators() method (in Number.cs), since original.Length is 0 :

int digitsStartPos = (original[0] == '-') ? 1 : 0;

We can see the same behaviour for all integral types except long and ulong.

josesimoes added a commit to Eclo/nf-interpreter that referenced this issue Sep 5, 2017
- fix nanoframework/Home#215
- fix nanoframework/Home#118

Signed-off-by: José Simões <jose.simoes@eclo.solutions>
josesimoes added a commit to nanoframework/nf-interpreter that referenced this issue Sep 5, 2017
- fix nanoframework/Home#215
- fix nanoframework/Home#118

Signed-off-by: José Simões <jose.simoes@eclo.solutions>
@ghost ghost removed the Status: FIXED label Dec 19, 2017
@ghost
Copy link

ghost commented Dec 19, 2017

Issue is still present, as the quick&dirty fix has been removed.

@ghost ghost reopened this Dec 19, 2017
@ghost
Copy link

ghost commented Dec 19, 2017

Looks like it is the expected behaviour, in fact. See last sentence :

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.

Taken from : http://www.cplusplus.com/reference/cstdio/printf/

The exception is not the expected behaviour on the managed side, however.

@josesimoes
Copy link
Member

Waiting for the PR to be submitted on the CoreLibrary repo.

ghost pushed a commit to nanoframework/CoreLibrary that referenced this issue Dec 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants