{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/GSL/issues/145","labels_url":"https://api.github.com/repos/Microsoft/GSL/issues/145/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/GSL/issues/145/comments","events_url":"https://api.github.com/repos/Microsoft/GSL/issues/145/events","html_url":"https://github.com/Microsoft/GSL/issues/145","id":111530299,"number":145,"title":"array_view as_bytes() and as_writeable_bytes() appear to invoke undefined behaviour in current implementation","user":{"login":"mattnewport","id":1325985,"avatar_url":"https://avatars.githubusercontent.com/u/1325985?v=3","gravatar_id":"","url":"https://api.github.com/users/mattnewport","html_url":"https://github.com/mattnewport","followers_url":"https://api.github.com/users/mattnewport/followers","following_url":"https://api.github.com/users/mattnewport/following{/other_user}","gists_url":"https://api.github.com/users/mattnewport/gists{/gist_id}","starred_url":"https://api.github.com/users/mattnewport/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattnewport/subscriptions","organizations_url":"https://api.github.com/users/mattnewport/orgs","repos_url":"https://api.github.com/users/mattnewport/repos","events_url":"https://api.github.com/users/mattnewport/events{/privacy}","received_events_url":"https://api.github.com/users/mattnewport/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":12,"created_at":"2015-10-15T02:05:17Z","updated_at":"2015-10-15T19:31:46Z","closed_at":null,"body":"The current implementations of `array_view`'s `as_bytes()` and `as_writeable_bytes()` do this:\r\n```C++\r\n// as_bytes()\r\nreturn { reinterpret_cast<const byte*>(this->data()), this->bytes() };\r\n// as_writeable_bytes()\r\nreturn { reinterpret_cast<byte*>(this->data()), this->bytes() };\r\n```\r\nWhere `byte` is defined as:\r\n```C++\r\nenum class byte : std::uint8_t {};\r\n```\r\nThis appears to invoke undefined behaviour if the `array_view` is used to access memory as per 3.10.10:\r\n> If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:\r\n- the dynamic type of the object,\r\n- a cv-qualified version of the dynamic type of the object,\r\n- a type similar (as defined in 4.4) to the dynamic type of the object,\r\n- a type that is the signed or unsigned type corresponding to the dynamic type of the object,\r\n- a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,\r\n- an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union),\r\n- a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,\r\n- **a `char` or `unsigned char` type.**\r\n\r\n\r\nThe usual get-out for this type of operation is the bolded clause (you are allowed to access any memory via a pointer to `char` or `unsigned char`) however I don't believe this applies because a) `uint8_t` is not guaranteed to be a typedef for either `char` or `unsigned char` (18.4.1):\r\n> `typedef` *unsigned integer type* `uint8_t`; // *optional*\r\n\r\nand b) an `enum` is a distinct type even if its storage is specified as a `char` type (7.2.1):\r\n> An enumeration is a distinct type (3.9.2) with named constants.\r\n\r\nIt's possible there is language elsewhere in the standard that makes this code not undefined behaviour but I can't find anything that would seem to make this well defined."},"comment":{"url":"https://api.github.com/repos/Microsoft/GSL/issues/comments/148498099","html_url":"https://github.com/Microsoft/GSL/issues/145#issuecomment-148498099","issue_url":"https://api.github.com/repos/Microsoft/GSL/issues/145","id":148498099,"user":{"login":"gdr-at-ms","id":11031650,"avatar_url":"https://avatars.githubusercontent.com/u/11031650?v=3","gravatar_id":"","url":"https://api.github.com/users/gdr-at-ms","html_url":"https://github.com/gdr-at-ms","followers_url":"https://api.github.com/users/gdr-at-ms/followers","following_url":"https://api.github.com/users/gdr-at-ms/following{/other_user}","gists_url":"https://api.github.com/users/gdr-at-ms/gists{/gist_id}","starred_url":"https://api.github.com/users/gdr-at-ms/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gdr-at-ms/subscriptions","organizations_url":"https://api.github.com/users/gdr-at-ms/orgs","repos_url":"https://api.github.com/users/gdr-at-ms/repos","events_url":"https://api.github.com/users/gdr-at-ms/events{/privacy}","received_events_url":"https://api.github.com/users/gdr-at-ms/received_events","type":"User","site_admin":false},"created_at":"2015-10-15T19:31:46Z","updated_at":"2015-10-15T19:31:46Z","body":"It is premature to assert that there is only one correct resolution."}}