Skip to content

Commit dbed593

Browse files
committed
Merge branch 'mh/maint-lockfile-overflow' into maint
* mh/maint-lockfile-overflow: lockfile: fix buffer overflow in path handling
2 parents 82ec54d + 2fbd4f9 commit dbed593

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lockfile.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,17 @@ static char *resolve_symlink(char *p, size_t s)
124124

125125
static int lock_file(struct lock_file *lk, const char *path, int flags)
126126
{
127-
if (strlen(path) >= sizeof(lk->filename))
128-
return -1;
129-
strcpy(lk->filename, path);
130127
/*
131128
* subtract 5 from size to make sure there's room for adding
132129
* ".lock" for the lock file name
133130
*/
131+
static const size_t max_path_len = sizeof(lk->filename) - 5;
132+
133+
if (strlen(path) >= max_path_len)
134+
return -1;
135+
strcpy(lk->filename, path);
134136
if (!(flags & LOCK_NODEREF))
135-
resolve_symlink(lk->filename, sizeof(lk->filename)-5);
137+
resolve_symlink(lk->filename, max_path_len);
136138
strcat(lk->filename, ".lock");
137139
lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
138140
if (0 <= lk->fd) {

0 commit comments

Comments
 (0)