Fix hardfaults when running out of memory (nullptr check)#26516
Fix hardfaults when running out of memory (nullptr check)#26516JonasPerolini wants to merge 2 commits intoPX4:mainfrom
Conversation
| _receiver.stop(); | ||
|
|
||
| delete _subscribe_to_stream; | ||
| delete[] _subscribe_to_stream; |
There was a problem hiding this comment.
Wondering if this is correct. Should we define the declaration to a char [] to make it clear?
There was a problem hiding this comment.
_subscribe_to_stream is allocated here
char *s = new char[n];
_subscribe_to_stream = s;
with new[] so we need to array delete it with [] (similar to delete[] s;)
Should we define the declaration to a char [] to make it clear?
We can't since the size is not fixed (strlen(stream_name) + 1)
There was a problem hiding this comment.
I think you're right :).
|
@julianoes please note that there are several other places where OOM can cause hard faults because nullptr checks are missing e.g.
How should we proceed? Can we handle all of them in the same PR? |
| if (_instance == nullptr) { | ||
| PX4_ERR("MavlinkCommandSender unavailable"); | ||
| static MavlinkCommandSender fallback; | ||
| _instance = &fallback; |
There was a problem hiding this comment.
I'm not sure how to handle this error. Any thoughts?
Solved Problem
Similar to #22056, when running out-of-memory, several parts of the code can cause a hardfault because a
nullptrcheck is missingnew char[n];returns null leading to a hardfault instrcpy(s, stream_name);Modules affected:
_instance = new MavlinkCommandSender();strcpy(s, stream_name);_uavcan_open_request_list_item *new_reqest = new _uavcan_open_request_list_item;_interpreter = new tflite::MicroInterpreter(control_model, resolver, tensor_arena, kTensorArenaSize);Subscription *subscription = new Subscription();next = new DataValidator();_sensor_gps_pubs[i] = new uORB::PublicationMulti<sensor_gps_s> {ORB_ID(sensor_gps)};_dist_pubs[i] = new uORB::PublicationMulti<distance_sensor_s> {ORB_ID(distance_sensor)};_repliers[_num_of_repliers] = replier;_vtol_type = new Tailsitter(this);_peak_magnitudes_allcheckChangelog Entry