@@ -156,6 +156,32 @@ define(function (require, exports, module) {
156156 await createDir ( folder ) ;
157157 }
158158
159+ function integrityCheck ( input ) {
160+ // The backup is of the form "length,string_backed_up" so that we can do integrity checks. ideally we should use
161+ // crypto hash functions but that may be expensive. since this is reversible with undo, not doing it for now.
162+ if ( ! input ) {
163+ return null ;
164+ }
165+ const parts = input . split ( ',' , 2 ) ;
166+
167+ if ( parts . length !== 2 ) {
168+ return null ;
169+ }
170+
171+ // Parse the length part (should be the first part before the comma)
172+ const expectedLength = parseInt ( parts [ 0 ] , 10 ) ;
173+ if ( isNaN ( expectedLength ) ) {
174+ return null ;
175+ }
176+
177+ // The second part is the actual string after the comma
178+ const actualString = parts [ 1 ] ;
179+ if ( actualString . length === expectedLength ) {
180+ return actualString ;
181+ }
182+ return null ;
183+ }
184+
159185 async function loadLastBackedUpFileContents ( projectRootPath ) {
160186 const project = trackedProjects [ projectRootPath ] ;
161187 if ( ! project ) {
@@ -171,7 +197,10 @@ define(function (require, exports, module) {
171197 if ( entry . isDirectory ) {
172198 continue ;
173199 }
174- let text = await jsPromise ( FileUtils . readAsText ( entry ) ) ;
200+ let text = integrityCheck ( await jsPromise ( FileUtils . readAsText ( entry ) ) ) ;
201+ if ( ! text ) {
202+ continue ;
203+ }
175204 let projectFilePath = getProjectFilePath ( entry . fullPath , projectRootPath ) ;
176205 if ( currentProjectLoadCount !== project . projectLoadCount ) {
177206 // this means that while we were tying to load a project backup, the user switched to another project
@@ -257,7 +286,8 @@ define(function (require, exports, module) {
257286 let parentDir = FileSystem . getDirectoryForPath ( path . dirname ( filePath ) ) ;
258287 await createDir ( parentDir ) ;
259288 let file = FileSystem . getFileForPath ( filePath ) ;
260- await jsPromise ( FileUtils . writeText ( file , contents , true ) ) ;
289+ const restoreContentsWithIntegrity = contents . length + "," + contents ;
290+ await jsPromise ( FileUtils . writeText ( file , restoreContentsWithIntegrity , true ) ) ;
261291 } catch ( e ) {
262292 console . error ( e ) ;
263293 }
0 commit comments