Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8362077

Browse files
authored
fix my byte-swapping implementation (#4772)
1 parent 4e3e156 commit 8362077

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

Python/import.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,21 +2197,21 @@ static PyObject *
21972197
_imp_source_hash_impl(PyObject *module, long key, Py_buffer *source)
21982198
/*[clinic end generated code: output=edb292448cf399ea input=9aaad1e590089789]*/
21992199
{
2200-
uint64_t hash = _Py_KeyedHash((uint64_t)key, source->buf, source->len);
2200+
union {
2201+
uint64_t x;
2202+
char data[sizeof(uint64_t)];
2203+
} hash;
2204+
hash.x = _Py_KeyedHash((uint64_t)key, source->buf, source->len);
22012205
#if !PY_LITTLE_ENDIAN
22022206
// Force to little-endian. There really ought to be a succinct standard way
22032207
// to do this.
2204-
union {
2205-
uint64_t x;
2206-
unsigned char data[sizeof(uint64_t)];
2207-
} pun;
2208-
pun.x = hash;
2209-
for (size_t i = 0; i < sizeof(pun.data); i++) {
2210-
pun.data[sizeof(pun.data) - i - 1] = pun.data[i];
2208+
for (size_t i = 0; i < sizeof(hash.data)/2; i++) {
2209+
char tmp = hash.data[i];
2210+
hash.data[i] = hash.data[sizeof(hash.data) - i - 1];
2211+
hash.data[sizeof(hash.data) - i - 1] = tmp;
22112212
}
2212-
hash = pun.x;
22132213
#endif
2214-
return PyBytes_FromStringAndSize((const char *)&hash, sizeof(hash));
2214+
return PyBytes_FromStringAndSize(hash.data, sizeof(hash.data));
22152215
}
22162216

22172217

0 commit comments

Comments
 (0)