Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saved files sometime only contains NUL-characters

We have a problem in our Windows 8.1 application (WinRT) that sometimes our saved file gets corrupt. The files have a correct file size, but the file only contains NUL-characters. The file should contain a serialized object as XML.

In an attempt to find the issue we do not overwrite the file, we do the following:

  1. Serialize the current object to a temp file.
  2. Check the content of the temp file
  3. Copy the current file (to .timestamp.xml.bak)
  4. Move/replace the temp file to the current file

Most of the time this all works fine, but sometimes the .timestamp.xml.bak-file and the content file get corrupt. Besides that, also the log file gets corrupt (also only NUL-characters). The whole file consists of NUL-characters. When I look at the trail of bak-files and the main file, I see that the main file is increased in size. That should be correct because a new XML-element is added. But it doesn’t contain XML.

I do not have a clue how and why this happens. It occurs in about 5% of files which should be edited and each corrupt file happens after 5-20 save attempts. It also happens on several tablets.

Here is a snippet of the code which creates the corrupt files:

StorageFile file = await lDataFld.CreateFileAsync(filename + ".tmp",  CreationCollisionOption.OpenIfExists);

StorageFile oldFile = await dataFld.GetFileAsync(filename + ".xml");
if (oldFile != null)
{
await oldFile.CopyAsync(dataFld, string.Format("{0}.{1}.xml.bak", filename, DateTime.Now.ToString("yyyyMMddHHmmssfffffff")), NameCollisionOption.ReplaceExisting);
}
await file.MoveAndReplaceAsync(await dataFld.GetFileAsync(filename + ".xml"));

Logger.Log(string.Format("Saved {0}.", filename));

Can someone tell me how we end up with files containing only NUL-characters and how/why this happens? And even better how it can be fixed.

A small adition: we cannot reproduce this issue in any way, it only occurs on our production environment.

like image 408
Bojo Avatar asked Aug 13 '14 14:08

Bojo


People also ask

What is NUL in files?

A file with a . null extension is a file that has been encrypted by the Null virus, which is a variant of Stop and Djvu ransomware that became prevalent in August 2017. It is encrypted with AES-256 algorithm so it is not possible to open the file by simply changing the . null file extension.

What is NUL in Notepad?

My font literally renders x00 as 0 over 0. As in the video, Notepad++ (64-bit) uses the letters N U L to represent the null, which is the issue.


1 Answers

The only reasons I can think of that may produce this result are:

  • The OS hard crashes (BSOD) in the middle of a file write
  • The application is terminated in the middle of a file write

I'm assuming it's the second reason.

Given this and the fact that you say it's a windows runtime application and I'm guessing the application is not being terminated manually, my guess is that it's something to do with the windows runtime lifecycle. Given that UWP application can be "suspended" when it goes to the background and then may be terminated by the OS at any point while in the "suspended" mode (very similar to mobile OSes like on Android or IOS).

So given this, my guess is that your application starts async operations before it gets suspended, then it gets suspended / terminated before it completes your file writing.

The fix is to make sure you mark your app saying it's doing "background" work while it's doing your file writes so that the OS will not suspend you during your file writing. Pretty much all mobile OSes have this type of feature to allow your to "overrun" just a little to make sure you can get your work done before your app is suspended / terminated. For UWP apps check out background tasks or look into how to handle app suspend / postponing app suspend.

like image 179
Shane Powell Avatar answered Oct 26 '22 23:10

Shane Powell