On this line, we are checking if a size_t variable bytes_written is <= 0. So even if the function mbedtls_x509write_crt_der returns a negative number to indicate error, we will never detect it.
We are lucky that mbedtls_x509write_crt_der never failed until today when I tried to generate an EEID based certificate and that triggered MBEDTLS_ERR_ASN1_BUF_TOO_SMALL.
An interesting questions is why the compiler did not complain that <= 0 does not make sense for a size_t variable.
It probably will actual complain if the expression is < 0. There is a 1/INT_MAX chance ==0 could catch an error.
size_t is the wrong type since mbedtls_x509write_crt_der can return a negative value on failure.
Follow up on Anand's question.
Most helpful comment
It probably will actual complain if the expression is
< 0. There is a1/INT_MAXchance==0could catch an error.