/* set filename */
t = time(NULL);
tmp = localtime(&t);
- if (tmp == NULL) {
+ if (NULL == tmp) {
+ printf("dlt_offline_trace_create_new_file: pointer to tmp is NULL!");
+ return -1;
}
if (strftime(outstr, sizeof(outstr),"%Y%m%d_%H%M%S", tmp) == 0) {
}
- sprintf(trace->filename,"%s/entrynav_%s.dlt",trace->directory,outstr);
+ sprintf(trace->filename,"%s/dlt_offlinetrace_%s.dlt",trace->directory,outstr);
/* open DLT output file */
trace->ohandle = open(trace->filename,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
/* go through all dlt files in directory */
DIR *dir = opendir(trace->directory);
while ((dp=readdir(dir)) != NULL) {
- if(strstr(dp->d_name,".dlt")) {
- sprintf(filename,"%s/%s",trace->directory,dp->d_name);
- stat(filename,&status);
- size += status.st_size;
- }
- }
+ if(strstr(dp->d_name,".dlt"))
+ {
+ int res = snprintf(filename, sizeof(filename), "%s/%s",trace->directory,dp->d_name);
+ // if the total length of the string is greater than the buffer, silently forget it.
+ // snprintf: a return value of size or more means that the output was truncated
+ // if an output error is encountered, a negative value is returned.
+ if( (unsigned int)res<sizeof(filename) && res>0 )
+ {
+ if(0 == stat(filename,&status))
+ {
+ size += status.st_size;
+ }
+ else
+ printf("Offline trace file %s cannot be stat-ed",filename);
+ }
+ //else
+ //{
+ // dlt_log(3, "dlt_offline_trace_get_total_size: long filename ignored");
+ //}
+ }
+ }
+
closedir(dir);
/* return size */
DIR *dir = opendir(trace->directory);
while ((dp=readdir(dir)) != NULL) {
if(strstr(dp->d_name,".dlt")) {
- sprintf(filename,"%s/%s",trace->directory,dp->d_name);
- stat(filename,&status);
- if(time_oldest == 0 || status.st_mtime < time_oldest) {
- time_oldest = status.st_mtime;
- size_oldest = status.st_size;
- strcpy(filename_oldest,filename);
- }
+ int res = snprintf(filename, sizeof(filename), "%s/%s",trace->directory,dp->d_name);
+ // if the total length of the string is greater than the buffer, silently forget it.
+ // snprintf: a return value of size or more means that the output was truncated
+ // if an output error is encountered, a negative value is returned.
+ if( (unsigned int)res<sizeof(filename) && res>0 )
+ {
+ if(0 == stat(filename,&status))
+ {
+ if(time_oldest == 0 || status.st_mtime < time_oldest) {
+ time_oldest = status.st_mtime;
+ size_oldest = status.st_size;
+ strcpy(filename_oldest,filename);
+ }
+ }
+ else
+ printf("Old offline trace file %s cannot be stat-ed",filename);
+ }
}
}
closedir(dir);
int dlt_offline_trace_init(DltOfflineTrace *trace,const char *directory,int fileSize,int maxSize) {
/* init parameters */
- strcpy(trace->directory,directory);
+ strncpy(trace->directory,directory,NAME_MAX);
trace->fileSize = fileSize;
trace->maxSize = maxSize;
{
/* close old file */
close(trace->ohandle);
+ trace->ohandle = -1;
/* check complete offline trace size, remove old logs if needed */
dlt_offline_trace_check_size(trace);
}
/* write data into log file */
- if(data1) {
+ if(data1 && (trace->ohandle >= 0)) {
if(write(trace->ohandle,data1,size1)!=size1) {
printf("Offline trace write failed!\n");
return -1;
}
}
- if(data2) {
+ if(data2 && (trace->ohandle >= 0)) {
if(write(trace->ohandle,data2,size2)!=size2) {
printf("Offline trace write failed!\n");
return -1;
}
}
- if(data3) {
+ if(data3 && (trace->ohandle >= 0)) {
if(write(trace->ohandle,data3,size3)!=size3) {
printf("Offline trace write failed!\n");
return -1;