Back to success stories

Sample of Defect

Project Name CID Checker Category Developer Description
ssurface 45163 RESOURCE_LEAK Resource leaks it was leading to a crash
File: /home/cardinot/subsurface/src/subsurface/datatrak.c
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
}


/*
 * Parses the dive extracting its data and filling a subsurface's dive structure
 */
static struct dive dt_dive_parser(FILE *archivo, struct dive *dt_dive)
{
        unsigned char n;
        int  profile_length;
        char *tmp_notes_str = NULL;
        unsigned char *tmp_string1 = NULL,
                      *locality = NULL,
                      *dive_point = NULL,
                      buffer[1024];
        struct divecomputer *dc = &dt_dive->dc;

        is_nitrox = is_O2 = is_SCR = 0;

        /*
         * Parse byte to byte till next dive entry
         */
        n = 0;
        fread(&lector_bytes[n], 1, 1, archivo);
 < 1. Condition "lector_bytes[n] != 160", taking true branch
 < 3. Condition "lector_bytes[n] != 160", taking true branch
 < 5. Condition "lector_bytes[n] != 160", taking false branch
179
        while (lector_bytes[n] != 0xA0)
 < 2. Jumping back to the beginning of the loop
 < 4. Jumping back to the beginning of the loop
180
181
182
183
184
185
                fread(&lector_bytes[n], 1, 1, archivo);

        /*
         * Found dive header 0xA000, verify second byte
         */
        fread(&lector_bytes[n+1], 1, 1, archivo);
 < 6. Condition "two_bytes_to_int(lector_bytes[0], lector_bytes[1]) != 40960", taking false branch
186
187
188
189
190
191
192
193
194
195
        if (two_bytes_to_int(lector_bytes[0], lector_bytes[1]) != 0xA000) {
                printf("Error: byte = %4x\n", two_bytes_to_int(lector_bytes[0], lector_bytes[1]));
                dt_dive = NULL;
                return *dt_dive;
        }

        /*
         * Begin parsing
         * First, Date of dive, 4 bytes
         */
 < 7. Switch case default
 < 8. Breaking from switch
196
197
198
199
200
201
        read_bytes(4);


        /*
         * Next, Time in minutes since 00:00
         */
 < 9. Switch case value "2"
 < 10. Breaking from switch
202
203
204
205
206
207
208
        read_bytes(2);

        dt_dive->dc.when = dt_dive->when = (timestamp_t)date_time_to_ssrfc(tmp_4bytes, tmp_2bytes);

        /*
         * Now, Locality, 1st byte is long of string, rest is string
         */
 < 11. Switch case value "1"
 < 12. Breaking from switch
209
210
211
212
213
214
        read_bytes(1);
        read_string(locality);

        /*
         * Next, Dive point, defined as Locality
         */
 < 13. Switch case value "1"
 < 14. Breaking from switch
215
216
217
218
219
220
221
222
223
        read_bytes(1);
        read_string(dive_point);

        /*
         * Subsurface only have a location variable, so we have to merge DTrak's
         * Locality and Dive points.
         */
        snprintf(buffer, sizeof(buffer), "%s, %s", locality, dive_point);
        dt_dive->dive_site_uuid = get_dive_site_uuid_by_name(buffer, NULL);
 < 15. Condition "dt_dive->dive_site_uuid == 0", taking true branch
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
        if (dt_dive->dive_site_uuid == 0)
                dt_dive->dive_site_uuid = create_dive_site(buffer);
        free(locality);
        free(dive_point);

        /*
         * Altitude. Don't exist in Subsurface, the equivalent would be
         * surface air pressure which can, be calculated from altitude.
         * As dtrak registers altitude intervals, we, arbitrarily, choose
         * the lower altitude/pressure equivalence for each segment. So
         *
         * Datatrak table            *  Conversion formula:
         *                           *
         * byte = 1   0 - 700 m      *  P = P0 * exp(-(g * M * h ) / (R * T0))
         * byte = 2   700 - 1700m    *  P0 = sealevel pressure = 101325 Pa
         * byte = 3   1700 - 2700 m  *  g = grav. acceleration = 9,80665 m/s²
         * byte = 4   2700 -  *   m  *  M = molar mass (dry air) = 0,0289644 Kg/mol
         *                           *  h = altitude over sea level (m)
         *                           *  R = Universal gas constant = 8,31447 J/(mol*K)
         *                           *  T0 = sea level standard temperature = 288,15 K
         */
 < 16. Switch case value "1"
 < 17. Breaking from switch
245
        read_bytes(1);
 < 18. Switch case value "1"
246
247
248
        switch (tmp_1byte) {
                case 1:
                        dt_dive->dc.surface_pressure.mbar = 1013;
 < 19. Breaking from switch
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
                        break;
                case 2:
                        dt_dive->dc.surface_pressure.mbar = 932;
                        break;
                case 3:
                        dt_dive->dc.surface_pressure.mbar = 828;
                        break;
                case 4:
                        dt_dive->dc.surface_pressure.mbar = 735;
                        break;
                default:
                        dt_dive->dc.surface_pressure.mbar = 1013;
        }

        /*
         * Interval (minutes)
         */
 < 20. Switch case value "2"
 < 21. Breaking from switch
266
        read_bytes(2);
 < 22. Condition "tmp_2bytes != 32767", taking true branch
267
268
269
270
271
272
273
274
        if (tmp_2bytes != 0x7FFF)
                dt_dive->dc.surfacetime.seconds = (uint32_t) tmp_2bytes * 60;

        /*
         * Weather, values table, 0 to 6
         * Subsurface don't have this record but we can use tags
         */
        dt_dive->tag_list = NULL;
 < 23. Switch case value "1"
 < 24. Breaking from switch
275
        read_bytes(1);
 < 25. Switch case value "1"
276
277
278
        switch (tmp_1byte) {
                case 1:
                        taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "clear")));
 < 26. Breaking from switch
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
                        break;
                case 2:
                        taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "misty")));
                        break;
                case 3:
                        taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "fog")));
                        break;
                case 4:
                        taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "rain")));
                        break;
                case 5:
                        taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "storm")));
                        break;
                case 6:
                        taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "snow")));
                        break;
                default:
                        // unknown, do nothing
                        break;
        }

        /*
         * Air Temperature
         */
 < 27. Switch case value "2"
 < 28. Breaking from switch
303
        read_bytes(2);
 < 29. Condition "tmp_2bytes != 32767", taking true branch
304
305
306
307
308
309
        if (tmp_2bytes != 0x7FFF)
                dt_dive->dc.airtemp.mkelvin = C_to_mkelvin((double)(tmp_2bytes / 100));

        /*
         * Dive suit, values table, 0 to 6
         */
 < 30. Switch case value "1"
 < 31. Breaking from switch
310
        read_bytes(1);
 < 32. Switch case value "1"
311
312
313
        switch (tmp_1byte) {
                case 1:
                        dt_dive->suit = strdup(QT_TRANSLATE_NOOP("gettextFromC", "No suit"));
 < 33. Breaking from switch
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
                        break;
                case 2:
                        dt_dive->suit = strdup(QT_TRANSLATE_NOOP("gettextFromC", "Shorty"));
                        break;
                case 3:
                        dt_dive->suit = strdup(QT_TRANSLATE_NOOP("gettextFromC", "Combi"));
                        break;
                case 4:
                        dt_dive->suit = strdup(QT_TRANSLATE_NOOP("gettextFromC", "Wet suit"));
                        break;
                case 5:
                        dt_dive->suit = strdup(QT_TRANSLATE_NOOP("gettextFromC", "Semidry suit"));
                        break;
                case 6:
                        dt_dive->suit = strdup(QT_TRANSLATE_NOOP("gettextFromC", "Dry suit"));
                        break;
                default:
                        // unknown, do nothing
                        break;
        }

        /*
         * Tank, volume size in liter*100. And initialize gasmix to air (default).
         * Dtrak don't record init and end pressures, but consumed bar, so let's
         * init a default pressure of 200 bar.
         */
 < 34. Switch case value "2"
 < 35. Breaking from switch
340
        read_bytes(2);
 < 36. Condition "tmp_2bytes != 32767", taking true branch
341
342
343
344
345
346
347
348
349
350
351
352
        if (tmp_2bytes != 0x7FFF) {
                dt_dive->cylinder[0].type.size.mliter = tmp_2bytes * 10;
                dt_dive->cylinder[0].type.description = strdup("");
                dt_dive->cylinder[0].start.mbar = 200000;
                dt_dive->cylinder[0].gasmix.he.permille = 0;
                dt_dive->cylinder[0].gasmix.o2.permille = 210;
                dt_dive->cylinder[0].manually_added = true;
        }

        /*
         * Maximum depth, in cm.
         */
 < 37. Switch case value "2"
 < 38. Breaking from switch
353
        read_bytes(2);
 < 39. Condition "tmp_2bytes != 32767", taking true branch
354
355
356
357
358
359
        if (tmp_2bytes != 0x7FFF)
                dt_dive->maxdepth.mm = dt_dive->dc.maxdepth.mm = (int32_t)tmp_2bytes * 10;

        /*
         * Dive time in minutes.
         */
 < 40. Switch case value "2"
 < 41. Breaking from switch
360
        read_bytes(2);
 < 42. Condition "tmp_2bytes != 32767", taking true branch
361
362
363
364
365
366
367
        if (tmp_2bytes != 0x7FFF)
                dt_dive->duration.seconds = dt_dive->dc.duration.seconds = (uint32_t)tmp_2bytes * 60;

        /*
         * Minimum water temperature in C*100. If unknown, set it to 0K which
         * is subsurface's value for "unknown"
         */
 < 43. Switch case value "2"
 < 44. Breaking from switch
368
        read_bytes(2);
 < 45. Condition "tmp_2bytes != 32767", taking true branch
369
        if (tmp_2bytes != 0x7fff)
 < 46. Falling through to end of if statement
370
371
372
373
374
375
376
                dt_dive->watertemp.mkelvin = dt_dive->dc.watertemp.mkelvin = C_to_mkelvin((double)(tmp_2bytes / 100));
        else
                dt_dive->watertemp.mkelvin = 0;

        /*
         * Air used in bar*100.
         */
 < 47. Switch case value "2"
 < 48. Breaking from switch
377
        read_bytes(2);
 < 49. Condition "tmp_2bytes != 32767", taking true branch
 < 50. Condition "dt_dive->cylinder[0].type.size.mliter", taking true branch
378
379
380
381
382
383
384
        if (tmp_2bytes != 0x7FFF && dt_dive->cylinder[0].type.size.mliter)
                dt_dive->cylinder[0].gas_used.mliter = dt_dive->cylinder[0].type.size.mliter * (tmp_2bytes / 100.0);

        /*
         * Dive Type 1 -  Bit table. Subsurface don't have this record, but
         * will use tags. Bits 0 and 1 are not used. Reuse coincident tags.
         */
 < 51. Switch case value "1"
 < 52. Breaking from switch
385
386
        read_bytes(1);
        byte = byte_to_bits(tmp_1byte);
 < 53. Condition "byte[2] != 0", taking true branch
387
388
        if (byte[2] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "no stop")));
 < 54. Condition "byte[3] != 0", taking true branch
389
390
        if (byte[3] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "deco")));
 < 55. Condition "byte[4] != 0", taking true branch
391
392
        if (byte[4] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "single ascent")));
 < 56. Condition "byte[5] != 0", taking true branch
393
394
        if (byte[5] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "multiple ascent")));
 < 57. Condition "byte[6] != 0", taking true branch
395
396
        if (byte[6] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "fresh")));
 < 58. Condition "byte[7] != 0", taking true branch
397
398
399
400
401
402
403
        if (byte[7] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "salt water")));
        free(byte);

        /*
         * Dive Type 2 - Bit table, use tags again
         */
 < 59. Switch case value "1"
 < 60. Breaking from switch
404
405
        read_bytes(1);
        byte = byte_to_bits(tmp_1byte);
 < 61. Condition "byte[0] != 0", taking true branch
406
407
408
409
        if (byte[0] != 0) {
                taglist_add_tag(&dt_dive->tag_list, strdup("nitrox"));
                is_nitrox = 1;
        }
 < 62. Condition "byte[1] != 0", taking true branch
410
411
412
413
414
415
416
417
418
419
        if (byte[1] != 0) {
                taglist_add_tag(&dt_dive->tag_list, strdup("rebreather"));
                is_SCR = 1;
                dt_dive->dc.divemode = PSCR;
        }
        free(byte);

        /*
         *  Dive Activity 1 - Bit table, use tags again
         */
 < 63. Switch case value "1"
 < 64. Breaking from switch
420
421
        read_bytes(1);
        byte = byte_to_bits(tmp_1byte);
 < 65. Condition "byte[0] != 0", taking true branch
422
423
        if (byte[0] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "sight seeing")));
 < 66. Condition "byte[1] != 0", taking true branch
424
425
        if (byte[1] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "club dive")));
 < 67. Condition "byte[2] != 0", taking true branch
426
427
        if (byte[2] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "instructor")));
 < 68. Condition "byte[3] != 0", taking true branch
428
429
        if (byte[3] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "instruction")));
 < 69. Condition "byte[4] != 0", taking true branch
430
431
        if (byte[4] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "night")));
 < 70. Condition "byte[5] != 0", taking true branch
432
433
        if (byte[5] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "cave")));
 < 71. Condition "byte[6] != 0", taking true branch
434
435
        if (byte[6] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "ice")));
 < 72. Condition "byte[7] != 0", taking true branch
436
437
438
439
440
441
442
443
        if (byte[7] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "search")));
        free(byte);


        /*
         * Dive Activity 2 - Bit table, use tags again
         */
 < 73. Switch case value "1"
 < 74. Breaking from switch
444
445
        read_bytes(1);
        byte = byte_to_bits(tmp_1byte);
 < 75. Condition "byte[0] != 0", taking true branch
446
447
        if (byte[0] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "wreck")));
 < 76. Condition "byte[1] != 0", taking true branch
448
449
        if (byte[1] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "river")));
 < 77. Condition "byte[2] != 0", taking true branch
450
451
        if (byte[2] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "drift")));
 < 78. Condition "byte[3] != 0", taking true branch
452
453
        if (byte[3] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "photo")));
 < 79. Condition "byte[4] != 0", taking true branch
454
455
456
457
458
459
460
461
        if (byte[4] != 0)
                taglist_add_tag(&dt_dive->tag_list, strdup(QT_TRANSLATE_NOOP("gettextFromC", "other")));
        free(byte);

        /*
         * Other activities - String  1st byte = long
         * Will put this in dive notes before the true notes
         */
 < 80. Switch case value "1"
 < 81. Breaking from switch
462
        read_bytes(1);
 < 82. Condition "tmp_1byte != 0", taking true branch
463
464
465
466
467
        if (tmp_1byte != 0) {
                read_string(tmp_string1);
                snprintf(buffer, sizeof(buffer), "%s: %s\n",
                         QT_TRANSLATE_NOOP("gettextFromC", "Other activities"),
                         tmp_string1);
 << 83. Storage is returned from allocation function "strdup".
 << 84. Assigning: "tmp_notes_str" = storage returned from "strdup(buffer)".
468
469
470
471
472
473
474
                tmp_notes_str = strdup(buffer);
                free(tmp_string1);
        }

        /*
         * Dive buddies
         */
 < 85. Switch case value "1"
 < 86. Breaking from switch
475
        read_bytes(1);
 < 87. Condition "tmp_1byte != 0", taking true branch
476
477
478
479
480
481
482
483
484
        if (tmp_1byte != 0) {
                read_string(tmp_string1);
                dt_dive->buddy = strdup(tmp_string1);
                free(tmp_string1);
        }

        /*
         * Dive notes
         */
 < 88. Switch case value "1"
 < 89. Breaking from switch
485
        read_bytes(1);
 < 90. Condition "tmp_1byte != 0", taking false branch
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
        if (tmp_1byte != 0) {
                read_string(tmp_string1);
                int len = snprintf(buffer, sizeof(buffer), "%s%s:\n%s",
                                   tmp_notes_str ? tmp_notes_str : "",
                                   QT_TRANSLATE_NOOP("gettextFromC", "Datatrak/Wlog notes"),
                                   tmp_string1);
                dt_dive->notes = calloc((len +1), 1);
                dt_dive->notes = memcpy(dt_dive->notes, buffer, len);
                free(tmp_string1);
                if (tmp_notes_str != NULL)
                        free(tmp_notes_str);
        }

        /*
         * Alarms 1 - Bit table - Not in Subsurface, we use the profile
         */
 < 91. Switch case value "1"
 < 92. Breaking from switch
502
503
504
505
506
        read_bytes(1);

        /*
         * Alarms 2 - Bit table - Not in Subsurface, we use the profile
         */
 < 93. Switch case value "1"
 < 94. Breaking from switch
507
508
509
510
511
        read_bytes(1);

        /*
         * Dive number  (in datatrak, after import user has to renumber)
         */
 < 95. Switch case value "2"
 < 96. Breaking from switch
512
513
514
515
516
517
        read_bytes(2);
        dt_dive->number = tmp_2bytes;

        /*
         * Computer timestamp - Useless for Subsurface
         */
 < 97. Switch case default
 < 98. Breaking from switch
518
519
520
521
522
523
524
525
        read_bytes(4);

        /*
         * Model - table - Not included 0x14, 0x24, 0x41, and 0x73
         * known to exist, but not its model name - To add in the future.
         * Strangely 0x00 serves for manually added dives and a dc too, at
         * least in EXAMPLE.LOG file, shipped with the software.
         */
 < 99. Switch case value "1"
 < 100. Breaking from switch
526
        read_bytes(1);
 < 101. Switch case value "0"
527
528
529
        switch (tmp_1byte) {
                case 0x00:
                        dt_dive->dc.model = strdup(QT_TRANSLATE_NOOP("gettextFromC", "Manually entered dive"));
 < 102. Breaking from switch
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
                        break;
                case 0x1C:
                        dt_dive->dc.model = strdup("Aladin Air");
                        break;
                case 0x1D:
                        dt_dive->dc.model = strdup("Spiro Monitor 2 plus");
                        break;
                case 0x1E:
                        dt_dive->dc.model = strdup("Aladin Sport");
                        break;
                case 0x1F:
                        dt_dive->dc.model = strdup("Aladin Pro");
                        break;
                case 0x34:
                        dt_dive->dc.model = strdup("Aladin Air X");
                        break;
                case 0x3D:
                        dt_dive->dc.model = strdup("Spiro Monitor 2 plus");
                        break;
                case 0x3F:
                        dt_dive->dc.model = strdup("Mares Genius");
                        break;
                case 0x44:
                        dt_dive->dc.model = strdup("Aladin Air X");
                        break;
                case 0x48:
                        dt_dive->dc.model = strdup("Spiro Monitor 3 Air");
                        break;
                case 0xA4:
                        dt_dive->dc.model = strdup("Aladin Air X O2");
                        break;
                case 0xB1:
                        dt_dive->dc.model = strdup("Citizen Hyper Aqualand");
                        break;
                case 0xB2:
                        dt_dive->dc.model = strdup("Citizen ProMaster");
                        break;
                case 0xB3:
                        dt_dive->dc.model = strdup("Mares Guardian");
                        break;
                case 0xBC:
                        dt_dive->dc.model = strdup("Aladin Air X Nitrox");
                        break;
                case 0xF4:
                        dt_dive->dc.model = strdup("Aladin Air X Nitrox");
                        break;
                case 0xFF:
                        dt_dive->dc.model = strdup("Aladin Pro Nitrox");
                        break;
                default:
                        dt_dive->dc.model = strdup(QT_TRANSLATE_NOOP("gettextFromC", "Unknown"));
                        break;
        }
 < 103. Condition "(tmp_1byte & 0xf0) == 0xf0", taking false branch
583
584
        if ((tmp_1byte & 0xF0) == 0xF0)
                is_nitrox = 1;
 < 104. Condition "(tmp_1byte & 0xf0) == 0xa0", taking false branch
585
586
587
588
589
590
591
        if ((tmp_1byte & 0xF0) == 0xA0)
                is_O2 = 1;

        /*
         * Air usage, unknown use. Probably allows or deny manually entering gas
         * comsumption based on dc model - Useless for Subsurface
         */
 < 105. Switch case value "1"
 < 106. Breaking from switch
592
593
594
595
596
597
        read_bytes(1);
        fseek(archivo, 6, 1);        // jump over 6 bytes whitout known use

        /*
         * Profile data length
         */
 < 107. Switch case value "2"
 < 108. Breaking from switch
598
599
        read_bytes(2);
        profile_length = tmp_2bytes;
 < 109. Condition "profile_length != 0", taking true branch
600
601
602
603
604
605
        if (profile_length != 0) {
                /*
                 * 8 x 2 bytes for the tissues saturation useless for subsurface
                 * and other 6 bytes without known use
                 */
                fseek(archivo, 22, 1);
 < 110. Condition "is_nitrox", taking true branch
606
607
608
609
610
                if (is_nitrox || is_O2) {

                        /*
                         * CNS  % (unsure) values table (only nitrox computers)
                         */
 < 111. Switch case value "1"
 < 112. Breaking from switch
611
612
613
614
615
                        read_bytes(1);

                        /*
                         * % O2 in nitrox mix - (only nitrox and O2 computers but differents)
                         */
 < 113. Switch case value "1"
 < 114. Breaking from switch
616
                        read_bytes(1);
 < 115. Condition "is_nitrox", taking true branch
617
                        if (is_nitrox) {
 < 116. Condition "tmp_1byte & 0xf", taking true branch
618
619
                                dt_dive->cylinder[0].gasmix.o2.permille =
                                        (tmp_1byte & 0x0F ? 20.0 + 2 * (tmp_1byte & 0x0F) : 21.0) * 10;
 < 117. Falling through to end of if statement
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
                        } else {
                                dt_dive->cylinder[0].gasmix.o2.permille = tmp_1byte * 10;
                                read_bytes(1)  // Jump over one byte, unknown use
                        }
                }
                /*
                 * profileLength = Nº bytes, need to know how many samples are there.
                 * 2bytes per sample plus another one each three samples. Also includes the
                 * bytes jumped over (22) and the nitrox (2) or O2 (3).
                 */
                int samplenum = is_O2 ? (profile_length - 25) * 3 / 8 : (profile_length - 24) * 3 / 7;

                dc->events = calloc(samplenum, sizeof(struct event));
                dc->alloc_samples = samplenum;
                dc->samples = 0;
                dc->sample = calloc(samplenum, sizeof(struct sample));

                dtrak_profile(dt_dive, archivo);
        }
        /*
         * Initialize some dive data not supported by Datatrak/WLog
         */
 < 118. Condition "!strcmp(dt_dive->dc.model, "Manually entered dive")", taking true branch
642
        if (!strcmp(dt_dive->dc.model, "Manually entered dive"))
 < 119. Falling through to end of if statement
643
644
645
646
647
                dt_dive->dc.deviceid = 0;
        else
                dt_dive->dc.deviceid = 0xffffffff;
        create_device_node(dt_dive->dc.model, dt_dive->dc.deviceid, "", "", dt_dive->dc.model);
        dt_dive->dc.next = NULL;
 < 120. Condition "!is_SCR", taking false branch
648
649
650
651
        if (!is_SCR && dt_dive->cylinder[0].type.size.mliter) {
                dt_dive->cylinder[0].end.mbar = dt_dive->cylinder[0].start.mbar -
                        ((dt_dive->cylinder[0].gas_used.mliter / dt_dive->cylinder[0].type.size.mliter) * 1000);
        }
 <<< CID 45163: Resource leaks RESOURCE_LEAK
 <<< 121. Variable "tmp_notes_str" going out of scope leaks the storage it points to.
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
        return *dt_dive;
}

void datatrak_import(const char *file, struct dive_table *table)
{
        FILE *archivo;
        dtrakheader *fileheader = (dtrakheader *)malloc(sizeof(dtrakheader));
        int i = 0;

        if ((archivo = subsurface_fopen(file, "rb")) == NULL) {
                report_error(translate("gettextFromC", "Error: couldn't open the file %s"), file);
                free(fileheader);
                return;
        }

        /*
         * Verify fileheader,  get number of dives in datatrak divelog
         */
        *fileheader = read_file_header(archivo);
        while (i < fileheader->divesNum) {
                struct dive *ptdive = alloc_dive();
                *ptdive = dt_dive_parser(archivo, ptdive);
                if (!ptdive)
                        report_error(translate("gettextFromC", "Error: no dive"));
                i++;
                record_dive(ptdive);
        }
Events:
83. alloc_fn datatrak.c:468
84. var_assign datatrak.c:468
121. leaked_storage datatrak.c:652