Skip to content

Brush up code style? #10501

@irwir

Description

@irwir

The suggestion came up in the issue #10448, but may have wider scope - the whole library code and example programs.

  1. Assignments in conditional expressions.
    This idiom remains in the library from the days of PolarSSL.
    It mostly helped to get efficient object code with relatively primitive compilers, but such conditions are harder to read and are known to be error-provoking.
    Extra set of parentheses usually suppresses compiler's warning, though static analysis still might flag it.

  2. Declaration of temporary variables where required, not at the function entrance (use C99 standard's advantages)
    Older programming languages required declaration to precede any code; and the library adheres to this obsolete and inconvenient pattern.
    Possible negative effects (and potential warnings) are: uninitialized variable, initialization with an unrelated value/assigned value was never used.
    More recent developments allow declarations anywhere. This makes code closer to RAII paradigm and easier to read because variable declaration would be nearby, not a few lines away. Then, lengthy list of declarations at the beginning of a function would get shorter.

The original code was:

int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;

if ((ret = mbedtls_pk_verify_new(sig_alg,
&ssl->session_negotiate->peer_cert->pk,
md_alg, verify_hash, verify_hash_len,
p, signature_len)) == 0) {
return 0;
}
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_new", ret);

Taking the above points into account, the code could be rewritten as:

    int ret = mbedtls_pk_verify_new(sig_alg,
                      &ssl->session_negotiate->peer_cert->pk,
                      md_alg, verify_hash, verify_hash_len,
                      p, signature_len);
    if (ret == 0) {
        return ret;
    }
    MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_new", ret);

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions