[apps/code] template program: don't memcpy from beyond the string

- Was caught by ASan
- Added a static_assert on the length just in case
- Big C strings concats replaced by raw C++ literal for clarity
This commit is contained in:
Adrien Bertrand
2017-08-31 20:52:25 +02:00
parent 925f404382
commit 3c2a99bdd6

View File

@@ -5,33 +5,33 @@ namespace Code {
Program::Program() :
m_buffer("")
{
const char * program =
const char program[] = R"(# This program draws a Mandelbrot fractal set
# N_iteration: degree of precision
import kandinsky
N_iteration = 10
for x in range(320):
for y in range(240):
# Compute the mandelbrot sequence for the point c = (c_r, c_i) with start value z = (z_r, z_i)
z_r = 0
z_i = 0
# Rescale to fit the drawing screen 320x222
c_r = 2.7*x/319-2.1
c_i = -1.87*y/221+0.935
i = 0
while (i < N_iteration) and ((z_r * z_r) + (z_i * z_i) < 4):
i = i + 1
stock = z_r
z_r = z_r * z_r - z_i * z_i + c_r
z_i = 2 * stock * z_i + c_i
# Choose the color of the dot from the Mandelbrot sequence
rgb = int(255*i/N_iteration)
col = kandinsky.color(int(rgb),int(rgb*0.75),int(rgb*0.25))
# Draw a pixel colored in 'col' at position (x,y)
kandinsky.set_pixel(x,y,col))";
"# This program draws a Mandelbrot fractal set\n"
"# N_iteration: degree of precision\n"
"import kandinsky\n"
"N_iteration = 10\n"
"for x in range(320):\n"
" for y in range(240):\n"
"# Compute the mandelbrot sequence for the point c = (c_r, c_i) with start value z = (z_r, z_i)\n"
" z_r = 0\n"
" z_i = 0\n"
"# Rescale to fit the drawing screen 320x222\n"
" c_r = 2.7*x/319-2.1\n"
" c_i = -1.87*y/221+0.935\n"
" i = 0\n"
" while (i < N_iteration) and ((z_r * z_r) + (z_i * z_i) < 4):\n"
" i = i + 1\n"
" stock = z_r\n"
" z_r = z_r * z_r - z_i * z_i + c_r\n"
" z_i = 2 * stock * z_i + c_i\n"
"# Choose the color of the dot from the Mandelbrot sequence\n"
" rgb = int(255*i/N_iteration)\n"
" col = kandinsky.color(int(rgb),int(rgb*0.75),int(rgb*0.25))\n"
"# Draw a pixel colored in 'col' at position (x,y)\n"
" kandinsky.set_pixel(x,y,col)\n";
memcpy(m_buffer, program, 1024);
constexpr size_t len = sizeof(program);
static_assert(len < k_bufferSize, "Default program length bigger than buffer size");
memcpy(m_buffer, program, len);
}
const char * Program::readOnlyContent() const {