Intel® FPGA University Program
University Program Material, Education Boards, and Laboratory Exercises
1174 Discussions

HAL code of Character Buffer for VGA Display (altera_up_avalon_video_character_buffer_with_dma) contains code that modifies string literal

BLee15
New Contributor I
1,122 Views

altera_up_avalon_video_character_buffer_with_dma.c contains following code:

 

void alt_up_char_buffer_init(alt_up_char_buffer_dev *char_buffer) { char * name; name = (char *) char_buffer->dev.name;   for ( ; (*name) != '\0'; name++) { if (strcmp(name, "_avalon_char_buffer_slave") == 0) { (*name) = '\0'; break; } } return; }

The type of dev.name is const char* , which (normally) contains string literal.​

 

This code does type conversion from const char* to char*, and then modifies contents of the string.

 

Therefore, this code is modifying contents of string literal, which causes undefined behavior.

 

 

I wrote the following code:

alt_up_char_buffer_dev* char_buffer = alt_up_char_buffer_open_dev(VIDEO_CHARACTER_BUFFER_AVALON_CHAR_BUFFER_SLAVE_NAME);

When I compile this code with -O0, the return value was NULL.

When I compile this code with -O2, the return value was pointer of (correct) device structure.

 

Version: Quartus Prime 18.1.1 Build 646 04/11/2019 SJ Lite Edition

0 Kudos
2 Replies
CheePin_C_Intel
Employee
792 Views

Hi,

 

As I understand it, you seems to have some inquiries related to video IP codes. Sorry for any confusion, to ensure we are on the same page, just would like to check with you on the following:

 

1. Would you mind to further elaborate on the specific IP that you are referring to here. Some reference to user guide would be helpful. 

 

2. I understand that you are observing some issue with codes. Would you mind to further elaborate on the issue observation and steps for replication. Some test design and screenshots would be helpful.

 

3. Just wonder if you are using any design example from Intel? If yes, mind refer me to it ie some web link for download would be helpful.

 

4. Would you mind to further elaborate on what is referred by "compile this code with -O0 or -O2"?

 

Please let me know if there is any concern. Thank you.

 

Best regards,

Chee Pin

 

0 Kudos
BLee15
New Contributor I
792 Views

1) The IP I'm referring is [Character Buffer for VGA Display] (altera_up_avalon_video_character_buffer_with_dma), which belongs to [University Program] / [Audio & Video] / [Video] on Platform Designer IP Catalog.

 

2) Here is main() function I used:

 

#include <stdlib.h>   #include "system.h" #include "altera_up_avalon_video_character_buffer_with_dma.h"   int main() { alt_up_char_buffer_dev* char_buffer = alt_up_char_buffer_open_dev(VIDEO_CHARACTER_BUFFER_AVALON_CHAR_BUFFER_SLAVE_NAME); if (char_buffer == NULL) return EXIT_FAILURE; alt_up_char_buffer_string(char_buffer, "Hello, world!", 0, 0); }

 

To avoid using pixel memory as code/data area, I modified some BSP configuration related to linker script.

 

bsp.png

 

 

When I use Level 2 optimization, name parameter inside alt_up_char_buffer_open_dev function is "/dev/video_character_buffer".

The parameter the caller site was VIDEO_CHARACTER_BUFFER_AVALON_CHAR_BUFFER_SLAVE_NAME, which was string literal "/dev/video_character_buffer_avalon_char_buffer_slave".

The return value is correct device descriptor.

 

debug_o2.png

 

 

When I use Optimization Off, name parameter inside alt_up_char_buffer_open_dev function is "/dev/video_character_buffer_avalon_char_buffer_slave".

The return value is NULL.

 

debug_o0.png

 

As I didn't expect that code behavior is different between different optimization level, I assumed that it is realted to infamous "undefined behavior", and examined what happened.

 

3) I'm not using any design examples. I uploaded QAR file, which is supposed to run with DE10-Lite board and 1280x1024 VGA monitor.

 

4) I meant "Optimization Level" here. "-O2" means "Level 2", and "-O0" means "Off".

 

optimization.png

 

 

optimization2.png

 

 

 

0 Kudos
Reply