[julia] fix result cache size

Commit dbd7618c9f removed the +1 from
width and height, resulting in out-of-bounds access in last row if -L is
used. Additionaly, the +1 only worked if -h or -w were used.

Fix all that tracking the width and hegith of result cache individually.
This commit is contained in:
Lucas 2020-03-08 23:12:59 +00:00
parent 2b9b56737a
commit fc9617b2b1
1 changed files with 19 additions and 15 deletions

34
julia.c
View File

@ -134,7 +134,7 @@ julia_main(int argc, char *argv[])
int palette_size = 720,
width = 640,
height = 400,
i, j;
rescache_width, rescache_height, i, j;
int ch, do_light = 0;
while ((ch = getopt(argc, argv, OPTSTRING)) != -1)
@ -191,7 +191,11 @@ julia_main(int argc, char *argv[])
palette = palette_gen(palette_size);
if ((rescache = malloc(sizeof(*rescache) * width * height)) == NULL)
rescache_width = width + (do_light ? 1 : 0);
rescache_height = height + (do_light ? 1 : 0);
if ((rescache = malloc(sizeof(*rescache) * rescache_width
* rescache_height)) == NULL)
errx(1, "out of memory");
if (do_light) {
@ -220,22 +224,22 @@ julia_main(int argc, char *argv[])
print_critical_points();
sec_w /= zoom; sec_h /= zoom;
for (i = 0; i < height; i++) {
for (i = 0; i < rescache_height; i++) {
zy = cen_y + (0.5L - i / (long double)height) * sec_h;
for (j = 0; j < width; j++) {
for (j = 0; j < rescache_width; j++) {
zx = cen_x + (j / (long double)width - 0.5L) * sec_w;
z = zx + I * zy;
it = iterate(z, julia, &rit);
rescache[i * width + j].it = it;
rescache[i * width + j].rit = rit;
rescache[i * rescache_width + j].it = it;
rescache[i * rescache_width + j].rit = rit;
}
}
pnmout_header(stdout, width - 1, height - 1);
for (i = 0; i < height - 1; i++) {
for (j = 0; j < width - 1; j++) {
it = rescache[i * width + j].it;
rit = rescache[i * width + j].rit;
pnmout_header(stdout, width, height);
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
it = rescache[i * rescache_width + j].it;
rit = rescache[i * rescache_width + j].rit;
if (it < 0)
color = contour_color;
@ -243,10 +247,10 @@ julia_main(int argc, char *argv[])
ritnum = (it - rit) * density;
if (do_light) {
long double lxrn, lyrn, l, lx, ly, lz;
lxrn = rescache[i * width + j + 1].it
- rescache[i * width + j + 1].rit;
lyrn = rescache[(i + 1) * width + j].it
- rescache[(i + 1) * width + j].rit;
lxrn = rescache[i * rescache_width + j + 1].it
- rescache[i * rescache_width + j + 1].rit;
lyrn = rescache[(i + 1) * rescache_width + j].it
- rescache[(i + 1) * rescache_width + j].rit;
lx = -sec_h * (lxrn - it + rit);
ly = sec_w * (lyrn - it + rit);
lz = sec_w * sec_h;