Skip to content

Commit

Permalink
externals/mbedtls: Use stat instead of fseek
Browse files Browse the repository at this point in the history
Use stat function instead of fseek function for binary file size
checking.
  • Loading branch information
SPRESENSE committed Mar 10, 2022
1 parent 5e2198d commit abe5d26
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
9 changes: 4 additions & 5 deletions externals/mbedtls/mbedtls-v2/library/dhm.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "mbedtls/error.h"

#include <string.h>
#include <sys/stat.h>

#if defined(MBEDTLS_PEM_PARSE_C)
#include "mbedtls/pem.h"
Expand Down Expand Up @@ -587,20 +588,18 @@ int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
static int load_file( const char *path, unsigned char **buf, size_t *n )
{
FILE *f;
long size;
struct stat st_buf;

if( ( f = fopen( path, "rb" ) ) == NULL )
return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );

fseek( f, 0, SEEK_END );
if( ( size = ftell( f ) ) == -1 )
if (stat(path, &st_buf) < 0)
{
fclose( f );
return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
}
fseek( f, 0, SEEK_SET );

*n = (size_t) size;
*n = (size_t) st_buf.st_size;

if( *n + 1 == 0 ||
( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Expand Down
12 changes: 9 additions & 3 deletions externals/mbedtls/mbedtls-v2/library/entropy.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "mbedtls/error.h"

#include <string.h>
#include <sys/stat.h>

#if defined(MBEDTLS_FS_IO)
#include <stdio.h>
Expand Down Expand Up @@ -510,13 +511,18 @@ int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *
FILE *f;
size_t n;
unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
struct stat st_buf;

if( ( f = fopen( path, "rb" ) ) == NULL )
return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );

fseek( f, 0, SEEK_END );
n = (size_t) ftell( f );
fseek( f, 0, SEEK_SET );
if (stat(path, &st_buf) < 0)
{
fclose(f);
return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
}

n = st_buf.st_size;

if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
Expand Down
9 changes: 4 additions & 5 deletions externals/mbedtls/mbedtls-v2/library/pkparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "mbedtls/error.h"

#include <string.h>
#include <sys/stat.h>

#if defined(MBEDTLS_RSA_C)
#include "mbedtls/rsa.h"
Expand Down Expand Up @@ -73,7 +74,7 @@
int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
{
FILE *f;
long size;
struct stat st_buf;

PK_VALIDATE_RET( path != NULL );
PK_VALIDATE_RET( buf != NULL );
Expand All @@ -82,15 +83,13 @@ int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
if( ( f = fopen( path, "rb" ) ) == NULL )
return( MBEDTLS_ERR_PK_FILE_IO_ERROR );

fseek( f, 0, SEEK_END );
if( ( size = ftell( f ) ) == -1 )
if (stat(path, &st_buf) < 0)
{
fclose( f );
return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
}
fseek( f, 0, SEEK_SET );

*n = (size_t) size;
*n = (size_t) st_buf.st_size;

if( *n + 1 == 0 ||
( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Expand Down

0 comments on commit abe5d26

Please sign in to comment.