classification
Title: resolve undefined behaviour in struct
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, meador.inge, serhiy.storchaka, xiang.zhang
Priority: normal Keywords:

Created on 2017-05-03 04:30 by xiang.zhang, last changed 2017-05-15 05:19 by xiang.zhang. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 1418 merged xiang.zhang, 2017-05-03 04:33
PR 1586 merged xiang.zhang, 2017-05-15 04:06
PR 1587 merged xiang.zhang, 2017-05-15 04:07
PR 1588 merged xiang.zhang, 2017-05-15 04:10
Messages (10)
msg292836 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-05-03 04:30
In struct there are several places using code like:

  p[--i] = (char)x;

to extract the least significant byte from a long. Although this behaviour seems to apply to common implementations but it does not conform to C standard.
msg292839 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-03 05:06
I think it is better to change the type of p to unsigned char.
msg292843 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-05-03 05:52
> I think it is better to change the type of p to unsigned char.

Good advice.
msg292844 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-03 06:20
I meant changing the type of the first argument of np_ubyte() etc.

np_ubyte(unsigned char *p, PyObject *v, const formatdef *f)
msg292845 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-05-03 06:25
Hmm, do we need to? Currently all pack routines accept a char *p. Converting it to unsigned char * inside could also solve the problem.
msg292847 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-03 07:03
Your patch LGTM as a minimal patch for backporting to maintained releases (maybe Mark have other comments). But I think it would be better to make all pointers to raw memory of type unsigned char*. Not just packing, but unpacking too. Using even memcpy between long* and char* is undefined behavior, the source and target should either have the same type, or one of them be unsigned char*.
msg293670 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-05-15 04:04
New changeset 981096f98b9c131594b0ac85ad01b63cbd11aa0a by Xiang Zhang in branch 'master':
bpo-30242: resolve some undefined behaviours in struct (#1418)
https://github.com/python/cpython/commit/981096f98b9c131594b0ac85ad01b63cbd11aa0a
msg293674 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-05-15 05:17
New changeset aad1caf55f7fadb00ee097a1465bece4e128d8ef by Xiang Zhang in branch '3.6':
bpo-30242: resolve some undefined behaviours in struct (#1418) (#1586)
https://github.com/python/cpython/commit/aad1caf55f7fadb00ee097a1465bece4e128d8ef
msg293675 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-05-15 05:17
New changeset dd2a09cf98845b1460f0e049ad0ffeeb5c6c6476 by Xiang Zhang in branch '3.5':
bpo-30242: resolve some undefined behaviours in struct (#1418) (#1587)
https://github.com/python/cpython/commit/dd2a09cf98845b1460f0e049ad0ffeeb5c6c6476
msg293676 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-05-15 05:17
New changeset 982a17e02d99dcf6e4dff93110cff5ecc59247f5 by Xiang Zhang in branch '2.7':
bpo-30242: resolve some undefined behaviours in struct (#1418) (#1588)
https://github.com/python/cpython/commit/982a17e02d99dcf6e4dff93110cff5ecc59247f5
History
Date User Action Args
2017-05-15 05:19:43xiang.zhangsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-05-15 05:17:56xiang.zhangsetmessages: + msg293676
2017-05-15 05:17:43xiang.zhangsetmessages: + msg293675
2017-05-15 05:17:30xiang.zhangsetmessages: + msg293674
2017-05-15 04:10:36xiang.zhangsetpull_requests: + pull_request1681
2017-05-15 04:07:50xiang.zhangsetpull_requests: + pull_request1680
2017-05-15 04:06:47xiang.zhangsetpull_requests: + pull_request1679
2017-05-15 04:04:28xiang.zhangsetmessages: + msg293670
2017-05-03 07:03:28serhiy.storchakasetmessages: + msg292847
2017-05-03 06:25:14xiang.zhangsetmessages: + msg292845
2017-05-03 06:20:41serhiy.storchakasetmessages: + msg292844
2017-05-03 05:52:50xiang.zhangsetmessages: + msg292843
2017-05-03 05:06:43serhiy.storchakasetmessages: + msg292839
2017-05-03 04:33:31xiang.zhangsetpull_requests: + pull_request1524
2017-05-03 04:30:53xiang.zhangcreate