@@ -301,6 +301,7 @@ static char *prepare_path(const char *path, char *full_path, int max_path)
301301
302302 // Reserve space for 2 backslashes and a NULL.
303303 strncpy (full_path , smb_curdir , max_path - 3 );
304+ full_path [max_path - 3 ] = '\0' ;
304305 strcat (full_path , "\\" );
305306
306307 // Skip all leading slashes and backslashes.
@@ -358,7 +359,8 @@ int smb_open(iop_file_t *f, const char *filename, int flags, int mode)
358359 fh -> filesize = 0 ;
359360 else if (fh -> mode & O_APPEND )
360361 fh -> position = filesize ;
361- strncpy (fh -> name , path , SMB_NAME_MAX );
362+ strncpy (fh -> name , path , SMB_NAME_MAX - 1 );
363+ fh -> name [SMB_NAME_MAX - 1 ] = '\0' ;
362364 r = 0 ;
363365 }
364366 } else
@@ -647,6 +649,10 @@ int smb_dopen(iop_file_t *f, const char *dirname)
647649 if (!(info .FileAttributes & EXT_ATTR_DIRECTORY )) {
648650 r = - ENOTDIR ;
649651 goto io_unlock ;
652+ }// The search pattern (path + optional backslash + '*') must fit in FHANDLE.name.
653+ if (strlen (path ) + 3 > SMB_NAME_MAX ) {
654+ r = - ENAMETOOLONG ;
655+ goto io_unlock ;
650656 }
651657
652658 fh = smbman_getfilefreeslot ();
@@ -657,7 +663,8 @@ int smb_dopen(iop_file_t *f, const char *dirname)
657663 fh -> filesize = 0 ;
658664 fh -> position = 0 ;
659665
660- strncpy (fh -> name , path , 255 );
666+ strncpy (fh -> name , path , SMB_NAME_MAX - 1 );
667+ fh -> name [SMB_NAME_MAX - 1 ] = '\0' ;
661668 if (fh -> name [strlen (fh -> name ) - 1 ] != '\\' )
662669 strcat (fh -> name , "\\" );
663670 strcat (fh -> name , "*" );
@@ -711,7 +718,7 @@ int smb_dread(iop_file_t *f, iox_dirent_t *dirent)
711718
712719 if (r == 1 ) {
713720 smb_statFiller (& info -> fileInfo , & dirent -> stat );
714- strncpy (dirent -> name , info -> FileName , SMB_NAME_MAX );
721+ strncpy (dirent -> name , info -> FileName , sizeof ( dirent -> name ) - 1 );
715722 }
716723
717724io_unlock :
0 commit comments