History

avcodec/lossless_videoencdsp: Fix unaligned access

HAVE_FAST_UNALIGNED being true does not imply that one can simply read from any pointer via *(long*). It is undefined behaviour in case the pointer is not sufficiently aligned; and even if it is, it is (likely) a violation of the effective-type rules. Fix both of these by using the appropriate AV_[RW]N macros. Also, the current code used sizeof(long) as if this were the CPU's native arithmetic size, but this is not true on 64bit Windows. This has been fixed, too. This affected huffyuv FATE-tests.

Andreas Rheinhardt committed 2 years ago

Commit cc2d2b61 parent 7a089ed1 file · +19 −8Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/lossless_videoencdsp.c+19−8
@@ -18,19 +18,31 @@ · 17 unmodified lines
1818
1919 #include "config.h"
2020 #include "libavutil/attributes.h"
21+#include "libavutil/intreadwrite.h"
2122 #include "lossless_videoencdsp.h"
2223 #include "mathops.h"
2324
25+#if HAVE_FAST_64BIT
26+typedef uint64_t uint_native;
27+#define READ AV_RN64
28+#define READA AV_RN64A
29+#define WRITEA AV_WN64A
30+#else
31+typedef uint32_t uint_native;
32+#define READ AV_RN32
33+#define READA AV_RN32A
34+#define WRITEA AV_WN32A
35+#endif
2436 // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size
25−#define pb_7f (~0UL / 255 * 0x7f)
26−#define pb_80 (~0UL / 255 * 0x80)
37+#define pb_7f (~(uint_native)0 / 255 * 0x7f)
38+#define pb_80 (~(uint_native)0 / 255 * 0x80)
2739
2840 static void diff_bytes_c(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, intptr_t w)
2941 {
3042 long i;
3143
3244 #if !HAVE_FAST_UNALIGNED
33− if (((long)src1 | (long)src2) & (sizeof(long) - 1)) {
45+ if (((uintptr_t)src1 | (uintptr_t)src2) & (sizeof(uint_native) - 1)) {
3446 for (i = 0; i + 7 < w; i += 8) {
3547 dst[i + 0] = src1[i + 0] - src2[i + 0];
3648 dst[i + 1] = src1[i + 1] - src2[i + 1];
@@ -43,11 +55,10 @@ static void diff_bytes_c(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, · 6 unmodified lines
4355 }
4456 } else
4557 #endif
46− for (i = 0; i <= w - (int) sizeof(long); i += sizeof(long)) {
47− long a = *(long *) (src1 + i);
48− long b = *(long *) (src2 + i);
49− *(long *) (dst + i) = ((a | pb_80) - (b & pb_7f)) ^
50− ((a ^ b ^ pb_80) & pb_80);
58+ for (i = 0; i <= w - (int) sizeof(uint_native); i += sizeof(uint_native)) {
59+ uint_native a = READA(src1 + i);
60+ uint_native b = READ(src2 + i);
61+ WRITEA(dst + i, ((a | pb_80) - (b & pb_7f)) ^ ((a ^ b ^ pb_80) & pb_80));
5162 }
5263 for (; i < w; i++)
5364 dst[i + 0] = src1[i + 0] - src2[i + 0];

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t