@@ -663,8 +663,11 @@ close_buffer(
663663 workshop_file_closed_lineno ((char * )buf -> b_ffname ,
664664 (int )buf -> b_last_cursor .lnum );
665665#endif
666- vim_free (buf -> b_ffname );
667- vim_free (buf -> b_sfname );
666+ if (buf -> b_sfname != buf -> b_ffname )
667+ VIM_CLEAR (buf -> b_sfname );
668+ else
669+ buf -> b_sfname = NULL ;
670+ VIM_CLEAR (buf -> b_ffname );
668671 if (buf -> b_prev == NULL )
669672 firstbuf = buf -> b_next ;
670673 else
@@ -1877,11 +1880,13 @@ curbuf_reusable(void)
18771880 */
18781881 buf_T *
18791882buflist_new (
1880- char_u * ffname , /* full path of fname or relative */
1881- char_u * sfname , /* short fname or NULL */
1882- linenr_T lnum , /* preferred cursor line */
1883- int flags ) /* BLN_ defines */
1883+ char_u * ffname_arg , // full path of fname or relative
1884+ char_u * sfname_arg , // short fname or NULL
1885+ linenr_T lnum , // preferred cursor line
1886+ int flags ) // BLN_ defines
18841887{
1888+ char_u * ffname = ffname_arg ;
1889+ char_u * sfname = sfname_arg ;
18851890 buf_T * buf ;
18861891#ifdef UNIX
18871892 stat_T st ;
@@ -1890,7 +1895,7 @@ buflist_new(
18901895 if (top_file_num == 1 )
18911896 hash_init (& buf_hashtab );
18921897
1893- fname_expand (curbuf , & ffname , & sfname ); /* will allocate ffname */
1898+ fname_expand (curbuf , & ffname , & sfname ); // will allocate ffname
18941899
18951900 /*
18961901 * If file name already exists in the list, update the entry.
@@ -1997,8 +2002,11 @@ buflist_new(
19972002 if ((ffname != NULL && (buf -> b_ffname == NULL || buf -> b_sfname == NULL ))
19982003 || buf -> b_wininfo == NULL )
19992004 {
2005+ if (buf -> b_sfname != buf -> b_ffname )
2006+ VIM_CLEAR (buf -> b_sfname );
2007+ else
2008+ buf -> b_sfname = NULL ;
20002009 VIM_CLEAR (buf -> b_ffname );
2001- VIM_CLEAR (buf -> b_sfname );
20022010 if (buf != curbuf )
20032011 free_buffer (buf );
20042012 return NULL ;
@@ -3103,18 +3111,21 @@ buflist_name_nr(
31033111}
31043112
31053113/*
3106- * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3114+ * Set the file name for "buf"' to "ffname_arg", short file name to
3115+ * "sfname_arg".
31073116 * The file name with the full path is also remembered, for when :cd is used.
31083117 * Returns FAIL for failure (file name already in use by other buffer)
31093118 * OK otherwise.
31103119 */
31113120 int
31123121setfname (
31133122 buf_T * buf ,
3114- char_u * ffname ,
3115- char_u * sfname ,
3123+ char_u * ffname_arg ,
3124+ char_u * sfname_arg ,
31163125 int message ) /* give message when buffer already exists */
31173126{
3127+ char_u * ffname = ffname_arg ;
3128+ char_u * sfname = sfname_arg ;
31183129 buf_T * obuf = NULL ;
31193130#ifdef UNIX
31203131 stat_T st ;
@@ -3123,8 +3134,11 @@ setfname(
31233134 if (ffname == NULL || * ffname == NUL )
31243135 {
31253136 /* Removing the name. */
3137+ if (buf -> b_sfname != buf -> b_ffname )
3138+ VIM_CLEAR (buf -> b_sfname );
3139+ else
3140+ buf -> b_sfname = NULL ;
31263141 VIM_CLEAR (buf -> b_ffname );
3127- VIM_CLEAR (buf -> b_sfname );
31283142#ifdef UNIX
31293143 st .st_dev = (dev_T )- 1 ;
31303144#endif
@@ -3175,8 +3189,9 @@ setfname(
31753189# endif
31763190 fname_case (sfname , 0 ); /* set correct case for short file name */
31773191#endif
3192+ if (buf -> b_sfname != buf -> b_ffname )
3193+ vim_free (buf -> b_sfname );
31783194 vim_free (buf -> b_ffname );
3179- vim_free (buf -> b_sfname );
31803195 buf -> b_ffname = ffname ;
31813196 buf -> b_sfname = sfname ;
31823197 }
@@ -3210,7 +3225,8 @@ buf_set_name(int fnum, char_u *name)
32103225 buf = buflist_findnr (fnum );
32113226 if (buf != NULL )
32123227 {
3213- vim_free (buf -> b_sfname );
3228+ if (buf -> b_sfname != buf -> b_ffname )
3229+ vim_free (buf -> b_sfname );
32143230 vim_free (buf -> b_ffname );
32153231 buf -> b_ffname = vim_strsave (name );
32163232 buf -> b_sfname = NULL ;
@@ -4820,27 +4836,31 @@ fix_fname(char_u *fname)
48204836}
48214837
48224838/*
4823- * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4824- * "ffname" becomes a pointer to allocated memory (or NULL).
4839+ * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
4840+ * "*ffname" becomes a pointer to allocated memory (or NULL).
4841+ * When resolving a link both "*sfname" and "*ffname" will point to the same
4842+ * allocated memory.
4843+ * The "*ffname" and "*sfname" pointer values on call will not be freed.
4844+ * Note that the resulting "*ffname" pointer should be considered not allocaed.
48254845 */
48264846 void
48274847fname_expand (
48284848 buf_T * buf UNUSED ,
48294849 char_u * * ffname ,
48304850 char_u * * sfname )
48314851{
4832- if (* ffname == NULL ) /* if no file name given, nothing to do */
4852+ if (* ffname == NULL ) // no file name given, nothing to do
48334853 return ;
4834- if (* sfname == NULL ) /* if no short file name given, use ffname */
4854+ if (* sfname == NULL ) // no short file name given, use ffname
48354855 * sfname = * ffname ;
4836- * ffname = fix_fname (* ffname ); /* expand to full path */
4856+ * ffname = fix_fname (* ffname ); // expand to full path
48374857
48384858#ifdef FEAT_SHORTCUT
48394859 if (!buf -> b_p_bin )
48404860 {
48414861 char_u * rfname ;
48424862
4843- /* If the file name is a shortcut file, use the file it links to. */
4863+ // If the file name is a shortcut file, use the file it links to.
48444864 rfname = mch_resolve_shortcut (* ffname );
48454865 if (rfname != NULL )
48464866 {
0 commit comments