@@ -1744,22 +1744,47 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
17441744 const char * env_psize = getenv ("WOLFBOOT_PARTITION_SIZE" );
17451745 const char * env_ssize = getenv ("WOLFBOOT_SECTOR_SIZE" );
17461746 if (env_psize ) {
1747- uint32_t partition_sz = (uint32_t )strtol (env_psize , NULL , 0 );
1748- uint32_t sector_sz = env_ssize ?
1749- (uint32_t )strtol (env_ssize , NULL , 0 ) : 0 ;
1750- uint32_t total_img_sz = CMD .header_sz + image_sz ;
1751- /* Only subtract sector for trailer when sector < partition.
1752- * When sector >= partition (e.g. update_ram targets), the
1753- * entire partition is available for the image. */
1754- uint32_t max_img_sz = (sector_sz < partition_sz ) ?
1755- (partition_sz - sector_sz ) : partition_sz ;
1756- if (total_img_sz > max_img_sz ) {
1757- printf ("Error: Image size %u (header %u + firmware %u) "
1758- "exceeds max %u (partition %u - sector %u)\n" ,
1759- total_img_sz , CMD .header_sz , image_sz ,
1760- max_img_sz , partition_sz , sector_sz );
1747+ char * endptr ;
1748+ unsigned long tmp ;
1749+ uint32_t partition_sz , sector_sz = 0 ;
1750+
1751+ errno = 0 ;
1752+ tmp = strtoul (env_psize , & endptr , 0 );
1753+ if (endptr == env_psize || * endptr != '\0' ||
1754+ errno == ERANGE || tmp == 0 || tmp > UINT32_MAX ) {
1755+ printf ("Error: Invalid WOLFBOOT_PARTITION_SIZE '%s'\n" ,
1756+ env_psize );
17611757 goto failure ;
17621758 }
1759+ partition_sz = (uint32_t )tmp ;
1760+
1761+ if (env_ssize ) {
1762+ errno = 0 ;
1763+ tmp = strtoul (env_ssize , & endptr , 0 );
1764+ if (endptr == env_ssize || * endptr != '\0' ||
1765+ errno == ERANGE || tmp == 0 || tmp > UINT32_MAX ) {
1766+ printf ("Error: Invalid WOLFBOOT_SECTOR_SIZE '%s'\n" ,
1767+ env_ssize );
1768+ goto failure ;
1769+ }
1770+ sector_sz = (uint32_t )tmp ;
1771+ }
1772+
1773+ {
1774+ uint32_t total_img_sz = CMD .header_sz + image_sz ;
1775+ /* Only subtract sector for trailer when sector < partition.
1776+ * When sector >= partition (e.g. update_ram targets), the
1777+ * entire partition is available for the image. */
1778+ uint32_t max_img_sz = (sector_sz < partition_sz ) ?
1779+ (partition_sz - sector_sz ) : partition_sz ;
1780+ if (total_img_sz > max_img_sz ) {
1781+ printf ("Error: Image size %u (header %u + firmware %u) "
1782+ "exceeds max %u (partition %u - sector %u)\n" ,
1783+ total_img_sz , CMD .header_sz , image_sz ,
1784+ max_img_sz , partition_sz , sector_sz );
1785+ goto failure ;
1786+ }
1787+ }
17631788 }
17641789 }
17651790
0 commit comments