Skip to content

Commit d880182

Browse files
committed
Merge pull request #2362 from pguyot/w29/fix-rp2-uart-driver
Bound rp2 uart read_blocking and avoid re-interning parity atoms 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 47f22bd + 5e803a4 commit d880182

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

src/platforms/rp2/src/lib/uartdriver.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ static term nif_uart_set_baudrate(Context *ctx, int argc, term argv[])
163163
return create_pair(ctx, OK_ATOM, term_from_int28((int32_t) actual));
164164
}
165165

166+
static const AtomStringIntPair parity_table[] = {
167+
{ ATOM_STR("\x4", "none"), UART_PARITY_NONE },
168+
{ ATOM_STR("\x4", "even"), UART_PARITY_EVEN },
169+
{ ATOM_STR("\x3", "odd"), UART_PARITY_ODD },
170+
SELECT_INT_DEFAULT(-1)
171+
};
172+
166173
static term nif_uart_set_format(Context *ctx, int argc, term argv[])
167174
{
168175
UNUSED(argc);
@@ -187,19 +194,12 @@ static term nif_uart_set_format(Context *ctx, int argc, term argv[])
187194
RAISE_ERROR(BADARG_ATOM);
188195
}
189196

190-
term parity_term = argv[3];
191-
uart_parity_t parity;
192-
if (parity_term == globalcontext_make_atom(ctx->global, ATOM_STR("\x4", "none"))) {
193-
parity = UART_PARITY_NONE;
194-
} else if (parity_term == globalcontext_make_atom(ctx->global, ATOM_STR("\x4", "even"))) {
195-
parity = UART_PARITY_EVEN;
196-
} else if (parity_term == globalcontext_make_atom(ctx->global, ATOM_STR("\x3", "odd"))) {
197-
parity = UART_PARITY_ODD;
198-
} else {
197+
int parity = interop_atom_term_select_int(parity_table, argv[3], ctx->global);
198+
if (UNLIKELY(parity < 0)) {
199199
RAISE_ERROR(BADARG_ATOM);
200200
}
201201

202-
uart_set_format(rsrc_obj->uart_inst, (uint) data_bits, (uint) stop_bits, parity);
202+
uart_set_format(rsrc_obj->uart_inst, (uint) data_bits, (uint) stop_bits, (uart_parity_t) parity);
203203

204204
return OK_ATOM;
205205
}
@@ -277,7 +277,7 @@ static term nif_uart_read_blocking(Context *ctx, int argc, term argv[])
277277
VALIDATE_VALUE(argv[1], term_is_integer);
278278

279279
avm_int_t count = term_to_int(argv[1]);
280-
if (UNLIKELY(count < 0)) {
280+
if (UNLIKELY(count < 0 || count > UINT16_MAX)) {
281281
RAISE_ERROR(BADARG_ATOM);
282282
}
283283

0 commit comments

Comments
 (0)