2222#include <stdint.h>
2323#include <string.h>
2424#include <delta.h>
25-
25+ #if defined(_MSC_VER )
26+ #include <limits.h> /* INT_MAX */
27+ #include <inttypes.h> /* PRIu32 */
28+ #endif
2629
2730#define ESC 0x7f
2831
@@ -181,17 +184,21 @@ int wb_diff_get_sector_size(void)
181184{
182185 uint32_t sec_sz = 0 ;
183186 char * env_sector_size = NULL ;
184- #ifdef _WIN32
187+ #if defined(_MSC_VER ) /* MSVC only, not _WIN32 that includes MSYS2/MinGW */
188+ char * dup = NULL ;
185189 size_t len = 0 ;
186- if (_dupenv_s (& env_sector_size , & len , "WOLFBOOT_SECTOR_SIZE" ) != 0 ) {
187- env_sector_size = NULL ; /* treat as not set */
190+ if (( _dupenv_s (& dup , & len , "WOLFBOOT_SECTOR_SIZE" ) == 0 ) && dup ) {
191+ env_sector_size = dup ;
188192 }
189193#else
190194 env_sector_size = getenv ("WOLFBOOT_SECTOR_SIZE" );
191195#endif
192196 if (!env_sector_size ) {
193197 fprintf (stderr , "Please set the WOLFBOOT_SECTOR_SIZE environment variable in\n"
194198 "order to sign a delta update.\n" );
199+ #if defined(_MSC_VER )
200+ free (dup );
201+ #endif
195202 exit (6 );
196203 } else {
197204 sec_sz = atoi (env_sector_size );
@@ -200,11 +207,28 @@ int wb_diff_get_sector_size(void)
200207 sec_sz = strtol (env_sector_size , NULL , 16 );
201208 if (errno != 0 ) {
202209 fprintf (stderr , "Invalid WOLFBOOT_SECTOR_SIZE value\n" );
210+ #if defined(_MSC_VER )
211+ free (dup );
212+ #endif
203213 exit (6 );
204214 }
205215 }
206216 }
207- return sec_sz ;
217+
218+ #if defined(_MSC_VER )
219+ free (dup );
220+ #endif
221+
222+ if (sec_sz == 0 ) {
223+ fprintf (stderr , "WOLFBOOT_SECTOR_SIZE cannot be 0\n" );
224+ exit (6 );
225+ }
226+ if (sec_sz > (uint32_t )INT_MAX ) {
227+ fprintf (stderr , "WOLFBOOT_SECTOR_SIZE (%" PRIu32 ") exceeds INT_MAX (%d)\n" ,
228+ sec_sz , INT_MAX );
229+ exit (6 );
230+ }
231+ return (int )sec_sz ;
208232}
209233
210234int wb_diff_init (WB_DIFF_CTX * ctx , uint8_t * src_a , uint32_t len_a , uint8_t * src_b , uint32_t len_b )
0 commit comments