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

cmd/compile: Incorrect conversion to string #13793

Closed
momchil-velikov opened this issue Dec 31, 2015 · 6 comments
Closed

cmd/compile: Incorrect conversion to string #13793

momchil-velikov opened this issue Dec 31, 2015 · 6 comments

Comments

@momchil-velikov
Copy link
Contributor

In the following program:

package main

import "fmt"

func main() {
    const A = 0xfffffffffffffffff // error
    // const A = 0x8000000000000000 // error
    // const A = 0x7fffffffffffffff // ok
    a := string(A)
    for i, r := range a {
        fmt.Printf("%d: %x\n", i, r)
    }
}

The compiler issues error message overflow in int -> string.

Form the Go Spec https://golang.org/ref/spec#Conversions

A constant value x can be converted to type T in any of these cases:

  • x is an integer constant and T is a string type. The same rule as for non-constant x applies in this case.

...

A non-constant value x can be converted to type T in any of these cases:

  • x is an integer or a slice of bytes or runes and T is a string type.

...

Conversions to and from a string type

  1. Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8
    representation of the integer. Values outside the range of valid Unicode code points are converted to
    "\uFFFD".

According to the above, the conversion should succeed, with the resulting string containing the UTF-8 encoding of 0xfffd.

gccgo does not issue an error and behaves as in the spec.

@cznic
Copy link
Contributor

cznic commented Dec 31, 2015

I think the problem is that A in

const A = 0xfffffffffffffffff

is not an integer constant (it's an untyped constant) nor can it be implicitly converted to any predeclared Go integer type. Then the specs part "x is an integer constant ..." does not apply.

@bradfitz
Copy link
Contributor

This may be a bug in gccgo, not in gc.
/cc @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

An "integer constant" is not limited to a predeclared Go integer type. There is no limit to the size of an integer constant. I think gccgo is technically correct and gc is technically wrong. gccgo actually has some code to specifically handle this case.

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Dec 31, 2015
@griesemer
Copy link
Contributor

The spec doesn't say the argument must be an integer constant (or that is must be representable as an int as the error message appears to suggest) - it just requires an (arbitrary) integer value. It says that values outside the Unicode code point range are converted to 0xfffd which is what should happen here.

I believe cmd/compile is incorrect; it should accept this code w/o error. FWIW, gotype also accepts this code (and - like gccgo - has code to handle this case specifically).

It should be ok to fix cmd/compile as it will not restrict the set of accepted programs (and thus is not an error that we must keep for backward-compatibility).

@griesemer griesemer changed the title cmd/compile: Incorrect converstion to string cmd/compile: Incorrect conversion to string Jan 4, 2016
@odeke-em
Copy link
Member

I believe this issue is a duplicate of #11330 /cc @dvyukov who reported it first.

@griesemer
Copy link
Contributor

@odeke-em You are correct. Closing as duplicate of #11330.

@golang golang locked and limited conversation to collaborators Aug 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants