]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/varint.c
Update bcachefs sources to bed61fae3b bcachefs: Delete a faulty assertion
[bcachefs-tools-debian] / libbcachefs / varint.c
index e6a041541792676d8936fdbc44dfac95314de076..2a2ab86ed6e1c7f09b9abe1994e8091e917665fc 100644 (file)
@@ -1,9 +1,14 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/bitops.h>
+#include <linux/math.h>
 #include <linux/string.h>
 #include <asm/unaligned.h>
 
+#ifdef CONFIG_VALGRIND
+#include <valgrind/memcheck.h>
+#endif
+
 #include "varint.h"
 
 /**
@@ -17,12 +22,13 @@ int bch2_varint_encode(u8 *out, u64 v)
 {
        unsigned bits = fls64(v|1);
        unsigned bytes = DIV_ROUND_UP(bits, 7);
+       __le64 v_le;
 
        if (likely(bytes < 9)) {
                v <<= bytes;
                v |= ~(~0 << (bytes - 1));
-               v = cpu_to_le64(v);
-               memcpy(out, &v, bytes);
+               v_le = cpu_to_le64(v);
+               memcpy(out, &v_le, bytes);
        } else {
                *out++ = 255;
                bytes = 9;
@@ -52,9 +58,10 @@ int bch2_varint_decode(const u8 *in, const u8 *end, u64 *out)
                return -1;
 
        if (likely(bytes < 9)) {
-               v = 0;
-               memcpy(&v, in, bytes);
-               v = le64_to_cpu(v);
+               __le64 v_le = 0;
+
+               memcpy(&v_le, in, bytes);
+               v = le64_to_cpu(v_le);
                v >>= bytes;
        } else {
                v = get_unaligned_le64(++in);
@@ -95,8 +102,11 @@ int bch2_varint_encode_fast(u8 *out, u64 v)
  */
 int bch2_varint_decode_fast(const u8 *in, const u8 *end, u64 *out)
 {
+#ifdef CONFIG_VALGRIND
+       VALGRIND_MAKE_MEM_DEFINED(in, 8);
+#endif
        u64 v = get_unaligned_le64(in);
-       unsigned bytes = ffz(v & 255) + 1;
+       unsigned bytes = ffz(*in) + 1;
 
        if (unlikely(in + bytes > end))
                return -1;