Fix cylinder gas type saving when we have bogus gas use data

Steve Williams reported a crash when saving a previously loaded dive as
xml, and gave a gdb backtrace.

It turns out that if we can't parse the cylinder use type (OC, diluent,
oxygen, unused) we initialize the cylinder use to an invalid type, and
then when we save it, we mess up.

Fix it up by doing proper limit checking before accessing the
"cylinderuse_text[]" array when saving.

Reported-by: Steve <stevewilliams@internode.on.net>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Linus Torvalds 2019-07-14 10:40:04 -07:00 committed by bstoeger
parent 3f2a73db7f
commit c685c05ff4
4 changed files with 8 additions and 6 deletions

View file

@ -140,6 +140,7 @@ static void save_cylinder_info(struct membuffer *b, struct dive *dive)
cylinder_t *cylinder = dive->cylinder + i;
int volume = cylinder->type.size.mliter;
const char *description = cylinder->type.description;
int use = cylinder->cylinder_use;
put_string(b, "cylinder");
if (volume)
@ -150,8 +151,8 @@ static void save_cylinder_info(struct membuffer *b, struct dive *dive)
put_gasmix(b, cylinder->gasmix);
put_pressure(b, cylinder->start, " start=", "bar");
put_pressure(b, cylinder->end, " end=", "bar");
if (cylinder->cylinder_use != OC_GAS)
put_format(b, " use=%s", cylinderuse_text[cylinder->cylinder_use]);
if (use > OC_GAS && use < NUM_GAS_USE)
show_utf8(b, " use=", cylinderuse_text[use], "");
if (cylinder->depth.mm != 0)
put_milli(b, " depth=", cylinder->depth.mm, "m");
put_string(b, "\n");