Skip to content

Commit 1ae6b07

Browse files
jrtc27borneoa
authored andcommitted
binarybuffer: Invert buf_cmp* return value and rename to buf_eq*
The current semantics are a bit confusing, as the return value looks like memcmp (0/false being equal) but the bool return type means one likely expects true to mean equal. Make this clearer by switching them out for buf_eq* functions that do that instead. Checkpatch-ignore: UNSPECIFIED_INT Change-Id: Iee0c5af794316aab5327cb9c168051fabd3bc1cb Signed-off-by: Jessica Clarke <[email protected]> Reviewed-on: https://review.openocd.org/c/openocd/+/8490 Tested-by: jenkins Reviewed-by: Evgeniy Naydanov <[email protected]> Reviewed-by: Antonio Borneo <[email protected]>
1 parent 5159c59 commit 1ae6b07

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

src/helper/binarybuffer.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,49 +57,49 @@ void *buf_cpy(const void *from, void *_to, unsigned size)
5757
return _to;
5858
}
5959

60-
static bool buf_cmp_masked(uint8_t a, uint8_t b, uint8_t m)
60+
static bool buf_eq_masked(uint8_t a, uint8_t b, uint8_t m)
6161
{
62-
return (a & m) != (b & m);
62+
return (a & m) == (b & m);
6363
}
64-
static bool buf_cmp_trailing(uint8_t a, uint8_t b, uint8_t m, unsigned trailing)
64+
static bool buf_eq_trailing(uint8_t a, uint8_t b, uint8_t m, unsigned trailing)
6565
{
6666
uint8_t mask = (1 << trailing) - 1;
67-
return buf_cmp_masked(a, b, mask & m);
67+
return buf_eq_masked(a, b, mask & m);
6868
}
6969

70-
bool buf_cmp(const void *_buf1, const void *_buf2, unsigned size)
70+
bool buf_eq(const void *_buf1, const void *_buf2, unsigned size)
7171
{
7272
if (!_buf1 || !_buf2)
73-
return _buf1 != _buf2;
73+
return _buf1 == _buf2;
7474

7575
unsigned last = size / 8;
7676
if (memcmp(_buf1, _buf2, last) != 0)
77-
return true;
77+
return false;
7878

7979
unsigned trailing = size % 8;
8080
if (!trailing)
81-
return false;
81+
return true;
8282

8383
const uint8_t *buf1 = _buf1, *buf2 = _buf2;
84-
return buf_cmp_trailing(buf1[last], buf2[last], 0xff, trailing);
84+
return buf_eq_trailing(buf1[last], buf2[last], 0xff, trailing);
8585
}
8686

87-
bool buf_cmp_mask(const void *_buf1, const void *_buf2,
87+
bool buf_eq_mask(const void *_buf1, const void *_buf2,
8888
const void *_mask, unsigned size)
8989
{
9090
if (!_buf1 || !_buf2)
91-
return _buf1 != _buf2 || _buf1 != _mask;
91+
return _buf1 == _buf2 && _buf1 == _mask;
9292

9393
const uint8_t *buf1 = _buf1, *buf2 = _buf2, *mask = _mask;
9494
unsigned last = size / 8;
9595
for (unsigned i = 0; i < last; i++) {
96-
if (buf_cmp_masked(buf1[i], buf2[i], mask[i]))
97-
return true;
96+
if (!buf_eq_masked(buf1[i], buf2[i], mask[i]))
97+
return false;
9898
}
9999
unsigned trailing = size % 8;
100100
if (!trailing)
101-
return false;
102-
return buf_cmp_trailing(buf1[last], buf2[last], mask[last], trailing);
101+
return true;
102+
return buf_eq_trailing(buf1[last], buf2[last], mask[last], trailing);
103103
}
104104

105105
void *buf_set_ones(void *_buf, unsigned size)

src/helper/binarybuffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ static inline uint64_t buf_get_u64(const uint8_t *_buffer,
172172
*/
173173
uint32_t flip_u32(uint32_t value, unsigned width);
174174

175-
bool buf_cmp(const void *buf1, const void *buf2, unsigned size);
176-
bool buf_cmp_mask(const void *buf1, const void *buf2,
175+
bool buf_eq(const void *buf1, const void *buf2, unsigned size);
176+
bool buf_eq_mask(const void *buf1, const void *buf2,
177177
const void *mask, unsigned size);
178178

179179
/**

src/jtag/core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,9 @@ static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
881881
int compare_failed;
882882

883883
if (in_check_mask)
884-
compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
884+
compare_failed = !buf_eq_mask(captured, in_check_value, in_check_mask, num_bits);
885885
else
886-
compare_failed = buf_cmp(captured, in_check_value, num_bits);
886+
compare_failed = !buf_eq(captured, in_check_value, num_bits);
887887

888888
if (compare_failed) {
889889
char *captured_str, *in_check_value_str;

src/svf/svf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ static int svf_check_tdo(void)
932932
index_var = svf_check_tdo_para[i].buffer_offset;
933933
len = svf_check_tdo_para[i].bit_len;
934934
if ((svf_check_tdo_para[i].enabled)
935-
&& buf_cmp_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var],
935+
&& !buf_eq_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var],
936936
&svf_mask_buffer[index_var], len)) {
937937
LOG_ERROR("tdo check error at line %d",
938938
svf_check_tdo_para[i].line_num);

0 commit comments

Comments
 (0)