Yun TFT Clock, issue combining time and touch


I am trying to make a modified version of this SMART alarm clock as my first Arduino project,

I am using the Yun and a adafruit 2.8 TFT. I am at the point where I have the display looking like i want it with the time displayed across the top and 3 buttons below but am having issues when combining the time updating and having touches register.

This issue only occurs when I print the timeString to the TFT.

void draw_display(int time) {
  // Print Time
  tft.setCursor(15, 30);
  char timeString[8];
  time_to_string(time, timeString);
  tft.print(timeString) ;
  // Print Refresh Button
  tft.drawRect(85, 130, 145, 40, ILI9341_WHITE);
  tft.setCursor(97, 139);
  // Print Cancel Button
  tft.drawRect(2, 200, 125, 40, ILI9341_WHITE);
  tft.setCursor(12, 209);
  // Print Option Button
  tft.drawRect(190, 200, 125, 40, ILI9341_WHITE);
  tft.setCursor(202, 209);

If I remove the tft.print(timeString); line, the buttons will appear and the touch will highlight as it is supposed to.

If I place a string in quotes in the place of timeString, (tft.print("any length of text here")) it will print that string and the buttons and the touch will highlight as its supposed to.

In all of these cases the clock will update as it is supposed to.

Here is related code to timeString

// Convert hour and minute to time string.  String MUST be a 9 character buffer!
void time_to_string(int time, char* timeString) {
  int hour12, minute;
  bool am;
  time_to_hour_minute(time, hour12, minute, am);
  timeString[8] = 0;
  timeString[7] = 'M';
  timeString[6] = am ? 'A' : 'P';
  timeString[5] = ' ';
  timeString[4] = '0' + (minute % 10);
  timeString[3] = '0' + (minute / 10);
  timeString[2] = ':';
  timeString[1] = '0' + (hour12 % 10);
  timeString[0] = (hour12 > 9) ? '1' : ' ';

Any help or advice would be much appreciated.

You have this comment in your time_to_string() function:

String MUST be a 9 character buffer!

So, inside draw_display(), why do you define the string to be only 8 characters?

 char timeString[8];

When you call time_to_string() to update timeString, it's over-running the timeString buffer and corrupting memory.

^ you are right. I believe i had messed with the char timeString line when trying to make the clock work originally and reading back through it I did not take into account timeString[0] and that timeString should have been 9 characters long.

Essentially the null character was missing so the print did not know when to stop.

Thank you for you help.

Essentially the null character was missing so the print did not know when to stop.

Actually, even though no space was allocated for it, the null terminator is still being assigned, and the print statement should work normally.

The issue is that being a local variable to the draw_display() function, eight characters for timeString are being allocated on the stack. But you are assigning nine values to it: when you are assigning the null terminator, you are indexing past the end of the allocated space, and overwriting something on the stack. You have no other local variables in that function, so that means you are overwriting something else, like a saved register value or the function return address - either one could cause bad and unpredictable things to happen when the draw_display() returns and that corrupted value is popped off of the stack.