Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/src/hal/comp.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,34 @@ If the requested number of instances exceeds the number of allowed personalities
personalities are assigned by indexing modulo the number of allowed personalities.
A message is printed denoting such assignments.

[NOTE]
====
If a component uses personality, then it should generally check its value.
The value of personality is zero if the 'personality=N' argument is not provided to loadrt.
Pins and params whose personality constraint evaluates to zero are not created, and their memory remains NULL.
Unconditionally accessing such a pin or param dereferences a NULL pointer and crashes the realtime process.

You can use a test in EXTRA_SETUP() to test the acceptable values of personality for your component.
You should return -EINVAL if your conditions are not met.

Example testing personality:

[source,c]
----
EXTRA_SETUP(){
// Silence warnings for the unused arguments.
(void)prefix;
(void)extra_arg;
Comment thread
grandixximo marked this conversation as resolved.
if (personality < 1) {
rtapi_print_msg(RTAPI_MSG_ERR,
"mycomp: personality must be >= 1 (use personality=N to set the number of channels)\n");
return -EINVAL;
}
return 0;
}
----
====

== Examples

=== constant
Expand Down
16 changes: 16 additions & 0 deletions src/hal/components/max31855.comp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ variable unsigned data_frame [15];
variable unsigned state = 1;

option period no;
option extra_setup yes;
function bitbang_spi;
license "GPL";
author "Joseph Calderon";
Expand Down Expand Up @@ -198,3 +199,18 @@ FUNCTION(bitbang_spi) {
state = (delay << 7) | (nbit << 1) | cs;
cs_out = cs;
}

EXTRA_SETUP(){
(void)prefix;
(void)extra_arg;
// The pins are sized by (personality & 0xf) but the function uses the
// raw value capped at 15, so a larger value exports fewer pins than
// the function accesses, and dereferencing the unexported pins kills
// rtapi_app when the function runs.
if (personality > 15) {
rtapi_print_msg(RTAPI_MSG_ERR,
"max31855: personality must be at most 15 (use personality=N to set the number of sensors)\n");
return -EINVAL;
}
return 0;
}
Loading