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
18 18
19#include "config.h"19#include "config.h"
20#include "libavutil/attributes.h"20#include "libavutil/attributes.h"
21#include "libavutil/intreadwrite.h"
21#include "lossless_videoencdsp.h"22#include "lossless_videoencdsp.h"
22#include "mathops.h"23#include "mathops.h"
23 24
25#if HAVE_FAST_64BIT
26typedef uint64_t uint_native;
27#define READ AV_RN64
28#define READA AV_RN64A
29#define WRITEA AV_WN64A
30#else
31typedef uint32_t uint_native;
32#define READ AV_RN32
33#define READA AV_RN32A
34#define WRITEA AV_WN32A
35#endif
24// 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size36// 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size
25#define pb_7f (~0UL / 255 * 0x7f)37#define pb_7f (~(uint_native)0 / 255 * 0x7f)
26#define pb_80 (~0UL / 255 * 0x80)38#define pb_80 (~(uint_native)0 / 255 * 0x80)
27 39
28static void diff_bytes_c(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, intptr_t w)40static void diff_bytes_c(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, intptr_t w)
29{41{
30 long i;42 long i;
31 43
32#if !HAVE_FAST_UNALIGNED44#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)) {
34 for (i = 0; i + 7 < w; i += 8) {46 for (i = 0; i + 7 < w; i += 8) {
35 dst[i + 0] = src1[i + 0] - src2[i + 0];47 dst[i + 0] = src1[i + 0] - src2[i + 0];
36 dst[i + 1] = src1[i + 1] - src2[i + 1];48 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
43 }55 }
44 } else56 } else
45#endif57#endif
46 for (i = 0; i <= w - (int) sizeof(long); i += sizeof(long)) {58 for (i = 0; i <= w - (int) sizeof(uint_native); i += sizeof(uint_native)) {
47 long a = *(long *) (src1 + i);59 uint_native a = READA(src1 + i);
48 long b = *(long *) (src2 + i);60 uint_native b = READ(src2 + i);
49 *(long *) (dst + i) = ((a | pb_80) - (b & pb_7f)) ^61 WRITEA(dst + i, ((a | pb_80) - (b & pb_7f)) ^ ((a ^ b ^ pb_80) & pb_80));
50 ((a ^ b ^ pb_80) & pb_80);
51 }62 }
52 for (; i < w; i++)63 for (; i < w; i++)
53 dst[i + 0] = src1[i + 0] - src2[i + 0];64 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