@@ -725,28 +725,31 @@ copy_ext_file(char *src_path, char *dst_path)
725725#define IS_DLEXT (e ) (strcmp((e), DLEXT) == 0)
726726
727727static void
728- fname_without_suffix (char * fname , char * rvalue )
728+ fname_without_suffix (const char * fname , char * rvalue , size_t rsize )
729729{
730- char * pos ;
731- strcpy ( rvalue , fname ) ;
732- for (pos = rvalue + strlen ( fname ) ; pos > rvalue ; pos -- ) {
730+ size_t len = strlen ( fname ) ;
731+ const char * pos ;
732+ for (pos = fname + len ; pos > fname ; pos -- ) {
733733 if (IS_SOEXT (pos ) || IS_DLEXT (pos )) {
734- * pos = '\0' ;
735- return ;
734+ len = pos - fname ;
735+ break ;
736736 }
737737 }
738+ if (len > rsize - 1 ) len = rsize - 1 ;
739+ memcpy (rvalue , fname , len );
740+ rvalue [len ] = '\0' ;
738741}
739742
740743static void
741- escaped_basename (char * path , char * fname , char * rvalue )
744+ escaped_basename (const char * path , const char * fname , char * rvalue , size_t rsize )
742745{
743- char * pos , * leaf , * found ;
744- leaf = path ;
746+ char * pos ;
747+ const char * leaf = path , * found ;
745748 // `leaf + 1` looks uncomfortable (when leaf == path), but fname must not be the top-dir itself
746749 while ((found = strstr (leaf + 1 , fname )) != NULL ) {
747750 leaf = found ; // find the last occurrence for the path like /etc/my-crazy-lib-dir/etc.so
748751 }
749- strcpy (rvalue , leaf );
752+ strlcpy (rvalue , leaf , rsize );
750753 for (pos = rvalue ; * pos ; pos ++ ) {
751754 if (isdirsep (* pos )) {
752755 * pos = '+' ;
@@ -762,8 +765,8 @@ rb_namespace_local_extension(VALUE namespace, VALUE fname, VALUE path)
762765 char * src_path = RSTRING_PTR (path ), * fname_ptr = RSTRING_PTR (fname );
763766 rb_namespace_t * ns = rb_get_namespace_t (namespace );
764767
765- fname_without_suffix (fname_ptr , fname2 );
766- escaped_basename (src_path , fname2 , basename );
768+ fname_without_suffix (fname_ptr , fname2 , sizeof ( fname2 ) );
769+ escaped_basename (src_path , fname2 , basename , sizeof ( basename ) );
767770
768771 wrote = sprint_ext_filename (ext_path , sizeof (ext_path ), ns -> ns_id , NAMESPACE_TMP_PREFIX , basename );
769772 if (wrote >= (int )sizeof (ext_path )) {
0 commit comments