Skip to content

Commit

Permalink
Add missing call to fclose
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbartell committed Jun 14, 2024
1 parent 1e46ce5 commit a6df290
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ target_include_directories( ${DEMO_NAME}
${CMAKE_CURRENT_LIST_DIR} )

set_macro_definitions(TARGETS ${DEMO_NAME}
OPTIONAL
"DOWNLOADED_CERT_WRITE_PATH"
"GENERATED_PRIVATE_KEY_WRITE_PATH"
REQUIRED
"AWS_IOT_ENDPOINT"
"ROOT_CA_CERT_PATH"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@
#include <unistd.h>
#include <errno.h>

#if defined( DOWNLOADED_CERT_WRITE_PATH )
#include <fcntl.h>
#endif // DOWNLOADED_CERT_WRITE_PATH

/* Demo config. */
#include "demo_config.h"

Expand Down Expand Up @@ -614,11 +610,17 @@ int main( int argc,
}
else
{
size_t itemsWritten = fwrite( certificate, certificateLength, 1, pFile );
if( itemsWritten != 1 )
size_t bytesWritten = fwrite( certificate, 1, certificateLength, pFile );
if( bytesWritten != certificateLength )
{
status = false;
LogError( ( "Failed to write device certificate to file %s.", CLIENT_CERT_PATH ) );
}
else
{
LogInfo( ( "Wrote client certificate to path: %s, length: %lu", CLIENT_CERT_PATH, ( unsigned long ) certificateLength ) );
}
fclose( pFile );
}
}

Expand Down Expand Up @@ -765,34 +767,6 @@ int main( int argc,
if( status == true )
{
LogInfo( ( "Demo completed successfully." ) );

#if defined( DOWNLOADED_CERT_WRITE_PATH )
{
int fd = open( DOWNLOADED_CERT_WRITE_PATH, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR );

if( -1 != fd )
{
const ssize_t writtenBytes = write( fd, certificate, certificateLength );

if( writtenBytes == certificateLength )
{
LogInfo( ( "Written %s successfully.", DOWNLOADED_CERT_WRITE_PATH ) );
}
else
{
LogError( ( "Could not write to %s. Error: %s.", DOWNLOADED_CERT_WRITE_PATH, strerror( errno ) ) );
}

close( fd );
}
else
{
LogError( ( "Could not open %s. Error: %s.", DOWNLOADED_CERT_WRITE_PATH, strerror( errno ) ) );
}
}
#else /* if defined( DOWNLOADED_CERT_WRITE_PATH ) */
LogInfo( ( "NOTE: define DOWNLOADED_CERT_WRITE_PATH in order to have the certificate written to disk." ) );
#endif // DOWNLOADED_CERT_WRITE_PATH
}

return ( status == true ) ? EXIT_SUCCESS : EXIT_FAILURE;
Expand Down

0 comments on commit a6df290

Please sign in to comment.