summaryrefslogtreecommitdiff
path: root/src/copy.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2001-10-06 16:43:07 +0000
committerJim Meyering <jim@meyering.net>2001-10-06 16:43:07 +0000
commit706a45c0b892a2775b916b4abe7441f2d83c34ca (patch)
tree9b40511c461378ce81d4a8bc78d8e7ce9e2627dd /src/copy.c
parent7bdd841fd252a6a6284f0fed51f6645a757aa3c6 (diff)
downloadcoreutils-706a45c0b892a2775b916b4abe7441f2d83c34ca.tar.xz
(record_dest): Avoid a small leak.
(copy_internal): Call remember_copied only for if the source file has 1 < st_nlink, or if it's a directory. Now that EARLIER_FILE is set conditionally, initialize it to NULL.
Diffstat (limited to 'src/copy.c')
-rw-r--r--src/copy.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/copy.c b/src/copy.c
index 5915c0e8b..c9d382567 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -673,8 +673,21 @@ record_dest (char const *dest, struct stat const *dest_stats)
ent->st_dev = stats.st_dev;
}
- if (! hash_insert (dest_info, ent))
- xalloc_die ();
+ {
+ struct Dest_info *ent_from_table = hash_insert (dest_info, ent);
+ if (ent_from_table == NULL)
+ {
+ /* Insertion failed due to lack of memory. */
+ xalloc_die ();
+ }
+
+ if (ent_from_table == ent)
+ {
+ /* There was alread a matching entry in the table, so ENT was
+ not inserted. Free it. */
+ free (ent);
+ }
+ }
}
/* Copy the file SRC_PATH to the file DST_PATH. The files may be of
@@ -703,7 +716,7 @@ copy_internal (const char *src_path, const char *dst_path,
struct stat dst_sb;
mode_t src_mode;
mode_t src_type;
- char *earlier_file;
+ char *earlier_file = NULL;
char *dst_backup = NULL;
int backup_succeeded = 0;
int rename_errno;
@@ -729,6 +742,8 @@ copy_internal (const char *src_path, const char *dst_path,
return 1;
}
+ src_type = src_sb.st_mode;
+
/* We wouldn't insert a node unless nlink > 1, except that we need to
find created files so as to not copy infinitely if a directory is
copied into itself. */
@@ -737,10 +752,10 @@ copy_internal (const char *src_path, const char *dst_path,
so that if we encounter a matching dev/ino pair in the source tree
we can arrange to create a hard link between the corresponding names
in the destination tree. */
- earlier_file = remember_copied (dst_path, src_sb.st_ino, src_sb.st_dev);
+ if (1 < src_sb.st_nlink || S_ISDIR (src_type))
+ earlier_file = remember_copied (dst_path, src_sb.st_ino, src_sb.st_dev);
src_mode = src_sb.st_mode;
- src_type = src_sb.st_mode;
if (S_ISDIR (src_type) && !x->recursive)
{