chaokunyang commented on code in PR #3216:
URL: https://github.com/apache/fory/pull/3216#discussion_r2730143855
##########
cpp/fory/python/pyfory.cc:
##########
@@ -58,4 +58,133 @@ int Fory_PyFloatSequenceWriteToBuffer(PyObject *collection,
Buffer *buffer,
}
return 0;
}
+
+// Write varint64 with ZigZag encoding inline
+// Returns number of bytes written
+static inline uint32_t WriteVarint64ZigZag(uint8_t *arr, int64_t value) {
+ // ZigZag encoding: (value << 1) ^ (value >> 63)
+ uint64_t v = (static_cast<uint64_t>(value) << 1) ^
+ (static_cast<uint64_t>(value >> 63));
+
+ if (v < 0x80) {
+ arr[0] = static_cast<uint8_t>(v);
+ return 1;
+ }
+ arr[0] = static_cast<uint8_t>((v & 0x7F) | 0x80);
+ if (v < 0x4000) {
+ arr[1] = static_cast<uint8_t>(v >> 7);
+ return 2;
+ }
+ arr[1] = static_cast<uint8_t>((v >> 7) | 0x80);
+ if (v < 0x200000) {
+ arr[2] = static_cast<uint8_t>(v >> 14);
+ return 3;
+ }
+ arr[2] = static_cast<uint8_t>((v >> 14) | 0x80);
+ if (v < 0x10000000) {
+ arr[3] = static_cast<uint8_t>(v >> 21);
+ return 4;
+ }
+ arr[3] = static_cast<uint8_t>((v >> 21) | 0x80);
+ if (v < 0x800000000ULL) {
+ arr[4] = static_cast<uint8_t>(v >> 28);
+ return 5;
+ }
+ arr[4] = static_cast<uint8_t>((v >> 28) | 0x80);
+ if (v < 0x40000000000ULL) {
+ arr[5] = static_cast<uint8_t>(v >> 35);
+ return 6;
+ }
+ arr[5] = static_cast<uint8_t>((v >> 35) | 0x80);
+ if (v < 0x2000000000000ULL) {
+ arr[6] = static_cast<uint8_t>(v >> 42);
+ return 7;
+ }
+ arr[6] = static_cast<uint8_t>((v >> 42) | 0x80);
+ if (v < 0x100000000000000ULL) {
+ arr[7] = static_cast<uint8_t>(v >> 49);
+ return 8;
+ }
+ arr[7] = static_cast<uint8_t>((v >> 49) | 0x80);
+ arr[8] = static_cast<uint8_t>(v >> 56);
+ return 9;
+}
+
+Py_ssize_t Fory_PyInt64SequenceWriteToBuffer(PyObject *collection,
Review Comment:
`Fory_PyInt64SequenceWriteToBuffer` returns -1 and sets PyErr when an
element is out of int64 range (or not an int), but the Cython caller only
checks bytes_written >= 0 and doesn’t propagate the exception. Because the
extern declaration has no except clause, the error is silently ignored and
the header is written without advancing writer_index, corrupting the stream
for lists with large Python ints (e.g., >2^63-1).
Please check bytes_written < 0 and raise/fall back to the slow path.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]