summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2011-11-15 07:29:46 +0100
committerChristophe Grenier <grenier@cgsecurity.org>2011-11-15 07:29:46 +0100
commitb3a8023e94cc629dbf838f27a6028ac57be90525 (patch)
tree7ff6521dd9bc9c234b05ba90ddd5128c3fa40c44
parent681a2531a2229c23e10275f8ee566201a729a3e1 (diff)
Fix a potential endless loop (PhotoRec 6.12 affected, not 6.11.3)
-rw-r--r--src/photorec.c49
-rw-r--r--src/photorec.h2
-rw-r--r--src/phrecn.c39
3 files changed, 45 insertions, 45 deletions
diff --git a/src/photorec.c b/src/photorec.c
index 6590e5b..7af903c 100644
--- a/src/photorec.c
+++ b/src/photorec.c
@@ -593,9 +593,9 @@ int file_finish(file_recovery_t *file_recovery, struct ph_param *params,
{
int file_recovered=0;
#ifdef DEBUG_FILE_FINISH
- log_debug("file_finish start %lu (%lu-%lu)\n", (long unsigned int)((*offset)/blocksize),
- (unsigned long int)((*current_search_space)->start/blocksize),
- (unsigned long int)((*current_search_space)->end/blocksize));
+ log_debug("file_finish start %lu (%lu-%lu)\n", (long unsigned int)((*offset)/params->blocksize),
+ (unsigned long int)((*current_search_space)->start/params->blocksize),
+ (unsigned long int)((*current_search_space)->end/params->blocksize));
log_debug("file_recovery->offset_error=%llu\n", (long long unsigned)file_recovery->offset_error);
log_debug("file_recovery->handle %s NULL\n", (file_recovery->handle!=NULL?"!=":"=="));
info_list_search_space(list_search_space, NULL, DEFAULT_SECTOR_SIZE, 0, 1);
@@ -673,17 +673,30 @@ int file_finish(file_recovery_t *file_recovery, struct ph_param *params,
else
reset_file_recovery(file_recovery);
#ifdef DEBUG_FILE_FINISH
- log_debug("file_finish end %lu (%lu-%lu)\n\n", (long unsigned int)((*offset)/blocksize),
- (unsigned long int)((*current_search_space)->start/blocksize),
- (unsigned long int)((*current_search_space)->end/blocksize));
+ log_debug("file_finish end %lu (%lu-%lu)\n\n", (long unsigned int)((*offset)/params->blocksize),
+ (unsigned long int)((*current_search_space)->start/params->blocksize),
+ (unsigned long int)((*current_search_space)->end/params->blocksize));
info_list_search_space(list_search_space, NULL, DEFAULT_SECTOR_SIZE, 0, 1);
#endif
return file_recovered;
}
-alloc_data_t *file_finish2(file_recovery_t *file_recovery, struct ph_param *params, const struct ph_options *options, alloc_data_t *list_search_space)
+/* file_finish2()
+ @param file_recovery -
+ @param struct ph_param *params
+ const struct ph_options *options
+ @param alloc_data_t *list_search_space
+ @param alloc_data_t **current_search_space
+ @param *offset
+
+ @returns:
+ -1: file not recovered, file_size=0 offset_error!=0
+ 0: file not recovered
+ 1: file recovered
+ */
+int file_finish2(file_recovery_t *file_recovery, struct ph_param *params, const struct ph_options *options, alloc_data_t *list_search_space, alloc_data_t **current_search_space, uint64_t *offset)
{
- alloc_data_t *datanext=NULL;
+ int file_recovered=0;
#ifdef DEBUG_FILE_FINISH
log_debug("file_recovery->offset_error=%llu\n", (long long unsigned)file_recovery->offset_error);
log_debug("file_recovery->handle %s NULL\n", (file_recovery->handle!=NULL?"!=":"=="));
@@ -750,14 +763,24 @@ alloc_data_t *file_finish2(file_recovery_t *file_recovery, struct ph_param *para
{
/* File hasn't been sucessfully recovered */
if(file_recovery->offset_error>0)
- datanext=file_error(list_search_space, file_recovery, params->blocksize);
+ {
+ alloc_data_t *datanext=file_error(list_search_space, file_recovery, params->blocksize);
+ if(datanext!=NULL)
+ {
+ *current_search_space=datanext;
+ *offset=datanext->start;
+ file_recovered=-1;
+ }
+ }
}
else
{
#ifdef ENABLE_DFXML
xml_log_file_recovered2(list_search_space, file_recovery);
#endif
- datanext=file_truncate(list_search_space, file_recovery, params->disk->sector_size, params->blocksize);
+ *current_search_space=file_truncate(list_search_space, file_recovery, params->disk->sector_size, params->blocksize);
+ *offset=(*current_search_space)->start;
+ file_recovered=1;
}
free_list_allocation(&file_recovery->location);
}
@@ -769,7 +792,7 @@ alloc_data_t *file_finish2(file_recovery_t *file_recovery, struct ph_param *para
#ifdef DEBUG_FILE_FINISH
info_list_search_space(list_search_space, NULL, DEFAULT_SECTOR_SIZE, 0, 1);
#endif
- return datanext;
+ return file_recovered;
}
void info_list_search_space(const alloc_data_t *list_search_space, const alloc_data_t *current_search_space, const unsigned int sector_size, const int keep_corrupted_file, const int verbose)
@@ -848,15 +871,13 @@ static alloc_data_t *file_truncate_aux(alloc_data_t *space, alloc_data_t *file,
static alloc_data_t *file_truncate(alloc_data_t *space, file_recovery_t *file, const unsigned int sector_size, const unsigned int blocksize)
{
- alloc_data_t *spacenext;
alloc_data_t *datanext;
if(file->filename!=NULL)
log_info("%s\t", file->filename);
else
log_info("?\t");
- spacenext=file_truncate_aux(space, file->loc, file->file_size, sector_size, blocksize);
+ datanext=file_truncate_aux(space, file->loc, file->file_size, sector_size, blocksize);
log_info("\n");
- datanext=td_list_entry(&spacenext->list.next, alloc_data_t, list);
return datanext;
}
diff --git a/src/photorec.h b/src/photorec.h
index e257217..217d059 100644
--- a/src/photorec.h
+++ b/src/photorec.h
@@ -60,7 +60,7 @@ struct ph_param
int get_prev_file_header(alloc_data_t *list_search_space, alloc_data_t **current_search_space, uint64_t *offset);
int file_finish(file_recovery_t *file_recovery, struct ph_param *params,
alloc_data_t *list_search_space, alloc_data_t **current_search_space, uint64_t *offset);
-alloc_data_t *file_finish2(file_recovery_t *file_recovery, struct ph_param *params, const struct ph_options *options, alloc_data_t *list_search_space);
+int file_finish2(file_recovery_t *file_recovery, struct ph_param *params, const struct ph_options *options, alloc_data_t *list_search_space, alloc_data_t **current_search_space, uint64_t *offset);
void write_stats_log(const file_stat_t *file_stats);
void write_stats_stdout(const file_stat_t *file_stats);
void update_stats(file_stat_t *file_stats, alloc_data_t *list_search_space);
diff --git a/src/phrecn.c b/src/phrecn.c
index c92bbbd..852a8a2 100644
--- a/src/phrecn.c
+++ b/src/phrecn.c
@@ -225,7 +225,7 @@ static int photorec_aux(struct ph_param *params, const struct ph_options *option
params->disk->pread(params->disk, buffer, READ_SIZE, offset);
while(current_search_space!=list_search_space)
{
- int move_next=1;
+ int file_recovered=0;
uint64_t old_offset=offset;
#ifdef DEBUG
log_debug("sector %llu\n",
@@ -286,19 +286,12 @@ static int photorec_aux(struct ph_param *params, const struct ph_options *option
if(options->verbose > 1)
log_trace("A known header has been found, recovery of the previous file is finished\n");
{
- alloc_data_t *datanext;
- datanext=file_finish2(&file_recovery, params, options, list_search_space);
- if(datanext!=NULL)
- {
- current_search_space=datanext;
- offset=current_search_space->start;
- move_next=0;
- }
+ file_recovered=file_finish2(&file_recovery, params, options, list_search_space, &current_search_space, &offset);
}
reset_file_recovery(&file_recovery);
if(options->lowmem > 0)
forget(list_search_space,current_search_space);
- if(move_next!=0)
+ if(file_recovered==0)
{
file_recovery_cpy(&file_recovery, &file_recovery_new);
if(options->verbose > 1)
@@ -411,14 +404,7 @@ static int photorec_aux(struct ph_param *params, const struct ph_options *option
}
if(res==2)
{
- alloc_data_t *datanext;
- datanext=file_finish2(&file_recovery, params, options, list_search_space);
- if(datanext!=NULL)
- {
- current_search_space=datanext;
- offset=current_search_space->start;
- move_next=0;
- }
+ file_recovered=file_finish2(&file_recovery, params, options, list_search_space, &current_search_space, &offset);
reset_file_recovery(&file_recovery);
if(options->lowmem > 0)
forget(list_search_space,current_search_space);
@@ -429,41 +415,34 @@ static int photorec_aux(struct ph_param *params, const struct ph_options *option
log_info("PhotoRec has been stopped\n");
current_search_space=list_search_space;
}
- else if(move_next!=0)
+ else if(file_recovered==0)
{
get_next_sector(list_search_space, &current_search_space,&offset,blocksize);
}
- else // if(move_next==0)
+ else if(file_recovered>0)
{
/* try to recover the previous file, otherwise stay at the current location */
get_prev_file_header(list_search_space, &current_search_space, &offset);
}
if(current_search_space==list_search_space)
{
- alloc_data_t *datanext;
#ifdef DEBUG_GET_NEXT_SECTOR
log_trace("current_search_space==list_search_space=%p (prev=%p,next=%p)\n",
current_search_space, current_search_space->list.prev, current_search_space->list.next);
log_trace("End of media\n");
#endif
- datanext=file_finish2(&file_recovery, params, options, list_search_space);
- if(datanext!=NULL)
- {
- current_search_space=datanext;
- offset=current_search_space->start;
- move_next=0;
- }
+ file_recovered=file_finish2(&file_recovery, params, options, list_search_space, &current_search_space, &offset);
reset_file_recovery(&file_recovery);
if(options->lowmem > 0)
forget(list_search_space,current_search_space);
}
buffer_olddata+=blocksize;
buffer+=blocksize;
- if(move_next==0 ||
+ if(file_recovered==1 ||
old_offset+blocksize!=offset ||
buffer+read_size>buffer_start+buffer_size)
{
- if(move_next==0)
+ if(file_recovered==1)
memset(buffer_start,0,blocksize);
else
memcpy(buffer_start,buffer_olddata,blocksize);