{"action":"created","issue":{"url":"https://api.github.com/repos/richox/libzling/issues/8","labels_url":"https://api.github.com/repos/richox/libzling/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/richox/libzling/issues/8/comments","events_url":"https://api.github.com/repos/richox/libzling/issues/8/events","html_url":"https://github.com/richox/libzling/issues/8","id":75054208,"number":8,"title":"Undefined behavior detected by ubsan","user":{"login":"nemequ","id":1151,"avatar_url":"https://avatars.githubusercontent.com/u/1151?v=3","gravatar_id":"","url":"https://api.github.com/users/nemequ","html_url":"https://github.com/nemequ","followers_url":"https://api.github.com/users/nemequ/followers","following_url":"https://api.github.com/users/nemequ/following{/other_user}","gists_url":"https://api.github.com/users/nemequ/gists{/gist_id}","starred_url":"https://api.github.com/users/nemequ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nemequ/subscriptions","organizations_url":"https://api.github.com/users/nemequ/orgs","repos_url":"https://api.github.com/users/nemequ/repos","events_url":"https://api.github.com/users/nemequ/events{/privacy}","received_events_url":"https://api.github.com/users/nemequ/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-05-11T02:06:40Z","updated_at":"2015-09-15T02:40:53Z","closed_at":null,"body":"ubsan with latest zling (870b761cc2860c400c9be2cd0f54a51aadf9594d):\r\n\r\n```\r\n$ ./zling_demo e ~/local/src/squash-benchmark/alice29.txt alice29.txt.zling && ./zling_demo d alice29.txt.zling alice29.txt\r\nzling:\r\n   light-weight lossless data compression utility\r\n   by Zhang Li <zhangli10 at baidu.com>\r\n\r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:46:62: runtime error: load of misaligned address 0x7f2305dbd012 for type 'unsigned char', which requires 4 byte alignment\r\n0x7f2305dbd012: note: pointer points here\r\n 00 00  0d 0a 0d 0a 0d 0a 0d 0a  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  41 4c 49 43 45 27\r\n              ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:60:51: runtime error: load of misaligned address 0x7f2305dbd01a for type 'unsigned char', which requires 4 byte alignment\r\n0x7f2305dbd01a: note: pointer points here\r\n 0d 0a  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  41 4c 49 43 45 27 53 20  41 44 56 45 4e 54\r\n              ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:60:51: runtime error: load of misaligned address 0x7f2305dbd019 for type 'unsigned char', which requires 4 byte alignment\r\n0x7f2305dbd019: note: pointer points here\r\n 0a 0d 0a  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  41 4c 49 43 45 27 53 20  41 44 56 45 4e\r\n              ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:63:69: runtime error: load of misaligned address 0x7f2305dbd01a for type 'unsigned char', which requires 4 byte alignment\r\n0x7f2305dbd01a: note: pointer points here\r\n 0d 0a  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  41 4c 49 43 45 27 53 20  41 44 56 45 4e 54\r\n              ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:63:69: runtime error: load of misaligned address 0x7f2305dbd019 for type 'unsigned char', which requires 4 byte alignment\r\n0x7f2305dbd019: note: pointer points here\r\n 0a 0d 0a  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  41 4c 49 43 45 27 53 20  41 44 56 45 4e\r\n              ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:68:66: runtime error: load of misaligned address 0x7f2305dbd025 for type 'unsigned char', which requires 2 byte alignment\r\n0x7f2305dbd025: note: pointer points here\r\n 20 20 20 20 20 20 20  41 4c 49 43 45 27 53 20  41 44 56 45 4e 54 55 52  45 53 20 49 4e 20 57 4f  4e\r\n             ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:230:62: runtime error: load of misaligned address 0x7f2305dbd026 for type 'unsigned char', which requires 4 byte alignment\r\n0x7f2305dbd026: note: pointer points here\r\n 20 20 20 20 20 20  41 4c 49 43 45 27 53 20  41 44 56 45 4e 54 55 52  45 53 20 49 4e 20 57 4f  4e 44\r\n             ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:230:62: runtime error: load of misaligned address 0x7f2305dbd025 for type 'unsigned char', which requires 4 byte alignment\r\n0x7f2305dbd025: note: pointer points here\r\n 20 20 20 20 20 20 20  41 4c 49 43 45 27 53 20  41 44 56 45 4e 54 55 52  45 53 20 49 4e 20 57 4f  4e\r\n             ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:68:66: runtime error: load of misaligned address 0x7f2305dbd059 for type 'unsigned char', which requires 2 byte alignment\r\n0x7f2305dbd059: note: pointer points here\r\n 20 20 20  20 20 20 20 20 20 20 20  20 20 20 20 20 20 4c 65  77 69 73 20 43 61 72 72  6f 6c 6c 0d 0a\r\n              ^ \r\n  0.15 MB =>   0.05 MB 35.30%, 0.322 sec, speed=0.473 MB/sec\r\nencode: 152089 => 53682, time=0.322 sec, speed=0.473 MB/sec\r\nzling:\r\n   light-weight lossless data compression utility\r\n   by Zhang Li <zhangli10 at baidu.com>\r\n\r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:83:96: runtime error: load of misaligned address 0x7fb92920f019 for type 'unsigned char', which requires 4 byte alignment\r\n0x7fb92920f019: note: pointer points here\r\n 0a 0d 0a  20 20 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00\r\n              ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:83:96: runtime error: store to misaligned address 0x7fb92920f01a for type 'unsigned char', which requires 4 byte alignment\r\n0x7fb92920f01a: note: pointer points here\r\n 0d 0a  20 20 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00\r\n              ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:88:96: runtime error: load of misaligned address 0x7fb92920f019 for type 'unsigned char', which requires 4 byte alignment\r\n0x7fb92920f019: note: pointer points here\r\n 0a 0d 0a  20 20 20 20 20 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00\r\n              ^ \r\n/home/nemequ/local/src/libzling/src/libzling_lz.cpp:88:96: runtime error: store to misaligned address 0x7fb92920f01d for type 'unsigned char', which requires 4 byte alignment\r\n0x7fb92920f01d: note: pointer points here\r\n 20 20 20 20 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00\r\n             ^ \r\n  0.15 MB <=   0.05 MB 35.30%, 0.098 sec, speed=1.553 MB/sec\r\ndecode: 152089 <= 53682, time=0.098 sec, speed=1.552 MB/sec\r\n```"},"comment":{"url":"https://api.github.com/repos/richox/libzling/issues/comments/140260615","html_url":"https://github.com/richox/libzling/issues/8#issuecomment-140260615","issue_url":"https://api.github.com/repos/richox/libzling/issues/8","id":140260615,"user":{"login":"nemequ","id":1151,"avatar_url":"https://avatars.githubusercontent.com/u/1151?v=3","gravatar_id":"","url":"https://api.github.com/users/nemequ","html_url":"https://github.com/nemequ","followers_url":"https://api.github.com/users/nemequ/followers","following_url":"https://api.github.com/users/nemequ/following{/other_user}","gists_url":"https://api.github.com/users/nemequ/gists{/gist_id}","starred_url":"https://api.github.com/users/nemequ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nemequ/subscriptions","organizations_url":"https://api.github.com/users/nemequ/orgs","repos_url":"https://api.github.com/users/nemequ/repos","events_url":"https://api.github.com/users/nemequ/events{/privacy}","received_events_url":"https://api.github.com/users/nemequ/received_events","type":"User","site_admin":false},"created_at":"2015-09-15T02:40:53Z","updated_at":"2015-09-15T02:40:53Z","body":"In that case,\r\n\r\n> This means a C++ compiler has no right to assume misaligned pointer conversions never occur as a C11-conforming compiler.\r\n\r\nSeems completely wrong.  If the behavior is implementation dependent, the implementation is free to assume misaligned pointer conversions never occur if that is the implementation they chose… which, AFAIK, is what they do (at least for C).  Since the range of possible behaviors is not enumerated in the standard, this seems like a perfectly valid assumption to make.\r\n\r\nMy understanding is that the only reason misaligned accesses often still work is that most instructions on x86, as well as newer ARM, allow misaligned access.  However certain instructions (like some vector instructions) don't, so you can easily run into trouble once the compiler gets better at optimizing your code (this recently happened to LZ4 with gcc ≥ 4.8).  And, of course, other architectures aren't as forgiving—[MIPS tends to have lots of trouble](https://www.linux-mips.org/wiki/Alignment)."}}