dsd-fme icon indicating copy to clipboard operation
dsd-fme copied to clipboard

getDateN() in ncurses version not working correctly

Open craftbyte opened this issue 10 months ago • 6 comments

If I select WAV file per-call in the ncurses version, the file names have weird beginnings instead of the expected date. Upon further inspection, I discovered that the issue lies in the use of the getDateN() function. Certain compiler will free character arrays in the function epilogue. This causes the returned string to already be nulled instead of containing a date. This can be observed at least on a stock RPi 5 with Raspbian.

To demonstrate this, I made the following example:

char * getDateN(void) {
  #ifdef AERO_BUILD
  char datename[80];
  #else
  char datename[99];
  #endif
  char * curr2;
  struct tm * to;
  time_t t;
  t = time(NULL);
  printf("time: %d\n", t);
  to = localtime( & t);
  printf("localtime: %d\n", to);
  strftime(datename, sizeof(datename), "%Y%m%d", to);
  curr2 = strtok(datename, " ");
  printf("datename: %s\n", datename);
  printf("curr2(%p): %s\n", curr2, curr2);
  return curr2;
}
int main(int argc, char** argv) {
	char* date = getDateN();
	printf("ret: %s (%p)\n", date, date);
	return 0;
}

The output upon executing is as follows:

time: 1713981393
localtime: 203320136
datename: 20240424
curr2(0x7fffd9dfc5a8): 20240424
ret:  (0x7fffd9dfc5a8)

I think we should move the getDateN function to take a char* argument that it fills with the date using strcpy. I also do not understand the use of 80/92 characted long dates, since strftime will always return a string of length 8. The use of strtok is also not needed in my opinion.

For reference: link to code

craftbyte avatar Apr 24 '24 18:04 craftbyte

I'll put it on the to-do list. If you need it done sooner, feel free to make a pull request on the aw_dev branch. Thanks.

lwvmobile avatar Apr 24 '24 18:04 lwvmobile

On reflection, might be easier to just mirror what is happening in gettime, like this:

char * getDate()
{
  char * curr = (char *) malloc(30);
  time_t t = time(NULL);
  struct tm * ptm = localtime(& t);
  sprintf(
    curr,
    "%04d-%02d-%02d",
    ptm->tm_year+1900,
    ptm->tm_mon+1,
    ptm->tm_mday
  );
  return curr;
}

Not sure what the optimal amount of memory to allocate is for this though.

lwvmobile avatar Apr 24 '24 18:04 lwvmobile

This would be a memory leak, if we dont free the char array.

craftbyte avatar Apr 24 '24 19:04 craftbyte

I made a branch to work on consolidating and fixing all the date and time related b.s., if you want to take a look see, make suggestions, etc.

Also @ilyacodes if you could can you review my work in progress, make sure I'm not being a dumb ass on this.

lwvmobile avatar Apr 24 '24 20:04 lwvmobile

@lwvmobile Reviewed with a handful of comments on the commit, mostly just nits. Looks sane and nice to have the functionality in one place.

ilyacodes avatar Apr 25 '24 13:04 ilyacodes

@craftbyte Try pulling and compiling the latest version, see if it fixes your issues on stock RPi 5 with Raspbian.

lwvmobile avatar Apr 27 '24 18:04 lwvmobile

Going to go ahead and close this as completed now.

lwvmobile avatar May 21 '24 14:05 lwvmobile