Skip to content

Commit

Permalink
Merge pull request #49 from ullbergm/Error-Checking-strtof
Browse files Browse the repository at this point in the history
Handle strtof parsing errors
  • Loading branch information
eamars authored May 22, 2024
2 parents 66b90af + 25559f9 commit 53cae0a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/and_scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ static float _decode_measurement_msg(scale_standard_data_format_t * msg) {
// Doesn't really matter though..

// Decode weight information
float weight = strtof(msg->data, NULL);
char *endptr;
float weight = strtof(msg->data, &endptr);

if( endptr == msg->data ) {
// Conversion failed
return nanf(msg->data);
}

return weight;
}
Expand Down
8 changes: 7 additions & 1 deletion src/gng_scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ static float _decode_measurement_msg(gngscale_standard_data_format_t * msg) {
}

// Decode weight information
float weight = strtof(msg->data, NULL);
char *endptr;
float weight = strtof(msg->data, &endptr);

if( endptr == msg->data ) {
// Conversion failed
return nanf(msg->data);
}

// Apply the sign
weight *= sign;
Expand Down
9 changes: 8 additions & 1 deletion src/steinberg_scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ scale_handle_t steinberg_scale_handle = {


static float _decode_measurement_msg(steinberg_sbs_data_format_t * msg) {
float weight = strtof(msg->data, NULL);
// Decode weight information
char *endptr;
float weight = strtof(msg->data, &endptr);

if( endptr == msg->data ) {
// Conversion failed
return nanf(msg->data);
}

return weight;
}
Expand Down

0 comments on commit 53cae0a

Please sign in to comment.