Skip to content

Commit 4c6cc78

Browse files
committed
Merge pull request #2256 from pguyot/w14/fix-binary-at
Fix sign of results of several `binary` functions These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents cef8c1a + 7c3212e commit 4c6cc78

5 files changed

Lines changed: 42 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ functions that default to `?ATOMVM_NVS_NS` are deprecated now).
712712
- Fixed numerous bugs in memory allocations that could crash the VM
713713
- Fixed SNTP support that had been broken in IDF 4.x builds
714714
- Fixed `erlang:send/2` not sending to registered name
715+
- Fixed sign of `binary:at/2`, `binary:first/1` and `binary:last/1` results
715716

716717
### Breaking Changes
717718

src/libAtomVM/nifs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,7 +3410,7 @@ static term nif_binary_at_2(Context *ctx, int argc, term argv[])
34103410
RAISE_ERROR(BADARG_ATOM);
34113411
}
34123412

3413-
return term_from_int11(term_binary_data(bin_term)[pos]);
3413+
return term_from_int11((uint8_t) term_binary_data(bin_term)[pos]);
34143414
}
34153415

34163416
static term nif_binary_copy(Context *ctx, int argc, term argv[])
@@ -3456,7 +3456,7 @@ static term nif_binary_first_1(Context *ctx, int argc, term argv[])
34563456
RAISE_ERROR(BADARG_ATOM);
34573457
}
34583458

3459-
return term_from_int11(term_binary_data(bin_term)[0]);
3459+
return term_from_int11((uint8_t) term_binary_data(bin_term)[0]);
34603460
}
34613461

34623462
static term nif_binary_last_1(Context *ctx, int argc, term argv[])
@@ -3473,7 +3473,7 @@ static term nif_binary_last_1(Context *ctx, int argc, term argv[])
34733473
RAISE_ERROR(BADARG_ATOM);
34743474
}
34753475

3476-
return term_from_int11(term_binary_data(bin_term)[size - 1]);
3476+
return term_from_int11((uint8_t) term_binary_data(bin_term)[size - 1]);
34773477
}
34783478

34793479
static term nif_binary_part_3(Context *ctx, int argc, term argv[])

tests/erlang_tests/binary_at_test.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
-export([start/0, id/1, atp10/1]).
2424

2525
start() ->
26-
atp10(id(<<"HelloWorld">>)) + atp10safe(id(<<"">>)) + atp10safe(42).
26+
atp10(id(<<"HelloWorld">>)) + atp10safe(id(<<"">>)) + atp10safe(42) +
27+
high_byte_test(id(<<200>>)).
2728

2829
atp10(Bin) ->
2930
binary:at(Bin, 4) + 10.
3031

32+
high_byte_test(Bin) ->
33+
200 = binary:at(Bin, 0),
34+
0.
35+
3136
atp10safe(Bin) ->
3237
try atp10(Bin) of
3338
_Any -> 1

tests/erlang_tests/binary_first_test.erl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,26 @@
2323
-export([start/0, id/1, firstp10/1]).
2424

2525
start() ->
26-
firstp10(id(<<"HelloWorld">>)) + firstp10safe(<<>>) + firstp10safe(42) + firstp10safe({<<>>}).
26+
firstp10(id(<<"HelloWorld">>)) + firstp10safe(<<>>) + firstp10safe(42) + firstp10safe({<<>>}) +
27+
high_byte_first_test(id(<<200, 1, 2>>)) +
28+
high_byte_first_boundary_test(id(<<128, 1, 2>>)) +
29+
high_byte_first_max_test(id(<<255, 1, 2>>)).
2730

2831
firstp10(Bin) ->
2932
binary:first(Bin) + 10.
3033

34+
high_byte_first_test(Bin) ->
35+
200 = binary:first(Bin),
36+
0.
37+
38+
high_byte_first_boundary_test(Bin) ->
39+
128 = binary:first(Bin),
40+
0.
41+
42+
high_byte_first_max_test(Bin) ->
43+
255 = binary:first(Bin),
44+
0.
45+
3146
firstp10safe(Bin) ->
3247
try firstp10(Bin) of
3348
_Any -> 1

tests/erlang_tests/binary_last_test.erl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,26 @@
2323
-export([start/0, id/1, lastp10/1]).
2424

2525
start() ->
26-
lastp10(id(<<"HelloWorld">>)) + lastp10safe(<<>>) + lastp10safe(42) + lastp10safe({<<>>}).
26+
lastp10(id(<<"HelloWorld">>)) + lastp10safe(<<>>) + lastp10safe(42) + lastp10safe({<<>>}) +
27+
high_byte_last_test(id(<<1, 2, 200>>)) +
28+
high_byte_last_boundary_test(id(<<1, 2, 128>>)) +
29+
high_byte_last_max_test(id(<<1, 2, 255>>)).
2730

2831
lastp10(Bin) ->
2932
binary:last(Bin) + 10.
3033

34+
high_byte_last_test(Bin) ->
35+
200 = binary:last(Bin),
36+
0.
37+
38+
high_byte_last_boundary_test(Bin) ->
39+
128 = binary:last(Bin),
40+
0.
41+
42+
high_byte_last_max_test(Bin) ->
43+
255 = binary:last(Bin),
44+
0.
45+
3146
lastp10safe(Bin) ->
3247
try lastp10(Bin) of
3348
_Any -> 1

0 commit comments

Comments
 (0)