From a8818cf779d3a32f2f52337c6f258c16719625a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Pierre=20Andr=C3=A9?= Date: Tue, 21 Sep 2021 10:53:16 +0200 Subject: [PATCH] Used a default usn when the former one cannot be retrieved When creating a new MFT record, the former seq_no and usn are retrieved to avoid the new one to be mistaken for the former one. This may not be possible when the record is used for the first time or after some bad error. In such situation use default values. --- libntfs-3g/mft.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/libntfs-3g/mft.c b/libntfs-3g/mft.c index d0a601ff..5052d1ec 100644 --- a/libntfs-3g/mft.c +++ b/libntfs-3g/mft.c @@ -5,7 +5,7 @@ * Copyright (c) 2004-2005 Richard Russon * Copyright (c) 2004-2008 Szabolcs Szakacsits * Copyright (c) 2005 Yura Pakhuchiy - * Copyright (c) 2014-2018 Jean-Pierre Andre + * Copyright (c) 2014-2021 Jean-Pierre Andre * * This program/include file is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as published @@ -1529,8 +1529,17 @@ found_free_rec: goto undo_mftbmp_alloc; } + /* + * Retrieve the former seq_no and usn so that the new record + * cannot be mistaken for the former one. + * However the original record may just be garbage, so + * use some sensible value when they cannot be retrieved. + */ seq_no = m->sequence_number; - usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)); + if (le16_to_cpu(m->usa_ofs) <= (NTFS_BLOCK_SIZE - 2)) + usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs & -2)); + else + usn = const_cpu_to_le16(1); if (ntfs_mft_record_layout(vol, bit, m)) { ntfs_log_error("Failed to re-format mft record.\n"); free(m);