Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for const error description strings #3176

Merged
merged 4 commits into from
Apr 21, 2020

Conversation

aggarg
Copy link
Contributor

@aggarg aggarg commented Apr 9, 2020

Problem

mbedtls_strerror is a utility function which converts an mbedTLS error code into a human readable string. It requires the caller to allocate a buffer every time an error code needs to be converted to a string. It is an overkill and a waste of RAM for resource constrained microcontrollers - where the most common use case is to use these strings for logging.

Solution

The proposed commit adds two functions:

const char * mbedtls_high_level_strerr( int error_code );
const char * mbedtls_low_level_strerr( int error_code );

The above two functions convert the high level and low level parts of an mbedTLS error code to human readable strings. They return a const pointer to an unmodifiable string which is not supposed to be modified by the caller and only to be used for logging purposes. The caller no longer needs to allocate a buffer.

Status

READY

Requires Backporting

NO

The proposed change is completely backward compatible as it does not change the existing mbedtls_strerror function and ensures that it continues to behave the same way.

Files

Modified Files:

  • include/mbedtls/error.h
  • scripts/data_files/error.fmt
  • scripts/generate_errors.pl

Auto Generated File:

  • library/error.c

The library/error.c is generated by running scripts/generate_errors.pl (which seems to be the way it is generated).

Testing

./test_suite_error 

Single low error .................................................. PASS
Single high error ................................................. PASS
Low and high error ................................................ PASS
Non existing high error ........................................... PASS
Non existing low error ............................................ PASS
Non existing low and high error ................................... PASS

----------------------------------------------------------------------------

PASSED (6 / 6 tests (0 skipped))

Signed-off-by: Gaurav Aggarwal [email protected]

Problem
-------
mbedtls_strerror is a utility function which converts an mbedTLS error code
into a human readable string. It requires the caller to allocate a buffer every
time an error code needs to be converted to a string. It is an overkill and a
waste of RAM for resource constrained microcontrollers - where the most common
use case is to use these strings for logging.

Solution
--------
The proposed commit adds two functions:

* const char * mbedtls_high_level_strerr( int error_code );
* const char * mbedtls_low_level_strerr( int error_code );

The above two functions convert the high level and low level parts of an mbedTLS
error code to human readable strings. They return a const pointer to an
unmodifiable string which is not supposed to be modified by the caller and only
to be used for logging purposes. The caller no longer needs to allocate a
buffer.

Backward Compatibility
----------------------
The proposed change is completely backward compatible as it does not change
the existing mbedtls_strerror function and ensures that it continues to behave
the same way.

Signed-off-by: Gaurav Aggarwal <[email protected]>
- Use switch case instead of loop to generate faster code
- Add #if defined to address compiler error

Signed-off-by: Gaurav Aggarwal <[email protected]>
@aggarg
Copy link
Contributor Author

aggarg commented Apr 10, 2020

I do not have access to the internal jenkins server. Can someone help me with what the error in the failed PR checks is?

@gilles-peskine-arm
Copy link
Contributor

Everything that should have passed has passed. The Mbed OS testing is currently broken, but that doesn't hold up PRs.

@aggarg
Copy link
Contributor Author

aggarg commented Apr 10, 2020

Everything that should have passed has passed. The Mbed OS testing is currently broken, but that doesn't hold up PRs.

Thank you. Would you please help me in getting the required approvals then?

@gilles-peskine-arm gilles-peskine-arm added component-platform Portability layer and build scripts enhancement needs-review Every commit must be reviewed by at least two team members, labels Apr 10, 2020
@gilles-peskine-arm
Copy link
Contributor

It's on our review dashboard now.

@aggarg
Copy link
Contributor Author

aggarg commented Apr 10, 2020

It's on our review dashboard now.

Thank you.

The presence of these markers in the original code was helpful to me in
figuring out that this portion of the code is auto-generated.
Therefore, I think those are useful and should be present.

Signed-off-by: Gaurav Aggarwal <[email protected]>
@danh-arm danh-arm added the good-first-issue Good for newcomers label Apr 14, 2020
@bensze01 bensze01 self-requested a review April 16, 2020 08:45
@aggarg
Copy link
Contributor Author

aggarg commented Apr 16, 2020

Ping - Request for review.

bensze01
bensze01 previously approved these changes Apr 20, 2020
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the design, except that I wonder if mbedtls_high_level_strerr and mbedtls_low_level_strerr should operate on the high/low part of the error code, rather than require it to solely consist of a high/low error code.

I'm requesting some minor documentation improvements. Comments on mbedtls_high_level_strerr also apply to mbedtls_low_level_strerr.

1. The functions mbedtls_high_level_strerr and mbedtls_low_level_strerr
   accept any error code and extract the high-level and low-level parts
   respectively.
2. Documentation updates.

Signed-off-by: Gaurav Aggarwal <[email protected]>
@aggarg
Copy link
Contributor Author

aggarg commented Apr 20, 2020

I'm ok with the design, except that I wonder if mbedtls_high_level_strerr and mbedtls_low_level_strerr should operate on the high/low part of the error code, rather than require it to solely consist of a high/low error code.

I'm requesting some minor documentation improvements. Comments on mbedtls_high_level_strerr also apply to mbedtls_low_level_strerr.

Thank you for the review. Yes, I think it is a good suggestion - I have updated mbedtls_high_level_strerr and mbedtls_low_level_strerr to accept an error code and extract the high-level and low-level parts respectively. Also, made all the suggested documentation updates.

@gilles-peskine-arm
Copy link
Contributor

Thanks for the update! The CI failures are due to a temporary problem on the development branch (a file that broke check-files.py and check-generated-files.sh) which is now fixed. The failures don't prevent getting relevant results for this PR and the CI can be considered passing.

@gilles-peskine-arm gilles-peskine-arm added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Apr 21, 2020
@gilles-peskine-arm gilles-peskine-arm merged commit dc9c47d into Mbed-TLS:development Apr 21, 2020
@irwir
Copy link
Contributor

irwir commented Apr 21, 2020

@gilles-peskine-arm
So finally someone was found who converted the scripts to generate switch statements, as was discussed long ago. :)

One additional remark.
It appear that error_description variable is not needed at all, and the code easily could be made even more concise as in the following example:

        case -(MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH):
            return( "XTEA - The data input has an invalid length" );
        case ...

Should there be no objections, I guess @aggarg could do this. Would you?

fengjixuchui referenced this pull request in fengjixuchui/mbedtls Apr 21, 2020
Merge pull request ARMmbed#3176 from aggarg/development
@aggarg
Copy link
Contributor Author

aggarg commented Apr 21, 2020

@gilles-peskine-arm
So finally someone was found who converted the scripts to generate switch statements, as was discussed long ago. :)

One additional remark.
It appear that error_description variable is not needed at all, and the code easily could be made even more concise as in the following example:

        case -(MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH):
            return( "XTEA - The data input has an invalid length" );
        case ...

Should there be no objections, I guess @aggarg could do this. Would you?

Sure, will do. I am habitual of writing functions with single exit point which is why this style.

gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this pull request Apr 21, 2020
aggarg added a commit to aggarg/mbedtls that referenced this pull request Apr 21, 2020
This was suggested on this PR: Mbed-TLS#3176

Signed-off-by: Gaurav Aggarwal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports component-platform Portability layer and build scripts enhancement good-first-issue Good for newcomers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants