malloc-fail.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #define _GNU_SOURCE
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <dlfcn.h>
  5. #include "qcms.h"
  6. const unsigned char gray_icc[] =
  7. {0x00, 0x00, 0x01, 0x70, 0x4E, 0x4B, 0x4F, 0x4E, 0x02, 0x20, 0x00, 0x00,
  8. 0x6D, 0x6E, 0x74, 0x72, 0x47, 0x52, 0x41, 0x59, 0x58, 0x59, 0x5A, 0x20,
  9. 0x07, 0xCE, 0x00, 0x0B, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10. 0x61, 0x63, 0x73, 0x70, 0x41, 0x50, 0x50, 0x4C, 0x00, 0x00, 0x00, 0x00,
  11. 0x6E, 0x6F, 0x6E, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  12. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF6, 0xD6,
  13. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xD3, 0x2D, 0x00, 0x00, 0x00, 0x00,
  14. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
  18. 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x4B,
  19. 0x77, 0x74, 0x70, 0x74, 0x00, 0x00, 0x01, 0x0C, 0x00, 0x00, 0x00, 0x14,
  20. 0x6B, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x20, 0x00, 0x00, 0x00, 0x0E,
  21. 0x63, 0x70, 0x72, 0x74, 0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x00, 0x2C,
  22. 0x62, 0x6B, 0x70, 0x74, 0x00, 0x00, 0x01, 0x5C, 0x00, 0x00, 0x00, 0x14,
  23. 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A,
  24. 0x4E, 0x69, 0x6B, 0x6F, 0x6E, 0x47, 0x72, 0x61, 0x79, 0x47, 0x31, 0x2E,
  25. 0x38, 0x20, 0x76, 0x34, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x33, 0x30, 0x30,
  26. 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27. 0x1A, 0x4E, 0x69, 0x6B, 0x6F, 0x6E, 0x47, 0x72, 0x61, 0x79, 0x47, 0x31,
  28. 0x2E, 0x38, 0x20, 0x76, 0x34, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x33, 0x30,
  29. 0x30, 0x30, 0x00, 0x00, 0x58, 0x59, 0x5A, 0x20, 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0xF3, 0x51, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x16, 0xCC,
  31. 0x63, 0x75, 0x72, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  32. 0x01, 0xCD, 0x00, 0x00, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00,
  33. 0x4E, 0x69, 0x6B, 0x6F, 0x6E, 0x20, 0x49, 0x6E, 0x63, 0x2E, 0x20, 0x26,
  34. 0x20, 0x4E, 0x69, 0x6B, 0x6F, 0x6E, 0x20, 0x43, 0x6F, 0x72, 0x70, 0x6F,
  35. 0x72, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x32, 0x30, 0x30, 0x31, 0x00,
  36. 0x58, 0x59, 0x5A, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  38. void test_gray(qcms_profile *output_profile)
  39. {
  40. unsigned char srct[3] = { 221, 79, 129};
  41. unsigned char outt[3];
  42. qcms_transform *transform;
  43. qcms_profile *gray_profile = qcms_profile_from_memory(gray_icc, sizeof(gray_icc));
  44. if (gray_profile) {
  45. transform = qcms_transform_create(gray_profile, QCMS_DATA_GRAY_8, output_profile, QCMS_DATA_RGB_8, QCMS_INTENT_PERCEPTUAL);
  46. if (transform) {
  47. qcms_transform_data(transform, srct, outt, 1);
  48. qcms_transform_release(transform);
  49. }
  50. qcms_profile_release(gray_profile);
  51. }
  52. }
  53. void test_gray_precache(qcms_profile *output_profile)
  54. {
  55. unsigned char srct[3] = { 221, 79, 129};
  56. unsigned char outt[3];
  57. qcms_transform *transform;
  58. qcms_profile *gray_profile = qcms_profile_from_memory(gray_icc, sizeof(gray_icc));
  59. qcms_profile_precache_output_transform(output_profile);
  60. if (gray_profile) {
  61. transform = qcms_transform_create(gray_profile, QCMS_DATA_GRAY_8, output_profile, QCMS_DATA_RGB_8, QCMS_INTENT_PERCEPTUAL);
  62. if (transform) {
  63. qcms_transform_data(transform, srct, outt, 1);
  64. qcms_transform_release(transform);
  65. }
  66. qcms_profile_release(gray_profile);
  67. }
  68. }
  69. void do_test(void) {
  70. unsigned char srct[4] = { 221, 79, 129, 92};
  71. unsigned char outt[4];
  72. qcms_transform *transform;
  73. qcms_profile *input_profile, *output_profile, *rgb;
  74. qcms_CIE_xyY white_point = { 0, 0, 1.};
  75. qcms_CIE_xyYTRIPLE primaries = { {.9, .3, 1.}, {.2, .4, 1.}, {.7, .4, 1.}};
  76. rgb = qcms_profile_create_rgb_with_gamma(white_point, primaries, 1.8);
  77. input_profile = qcms_profile_sRGB();
  78. output_profile = qcms_profile_sRGB();
  79. if (output_profile) {
  80. test_gray(output_profile);
  81. test_gray_precache(output_profile);
  82. qcms_profile_release(output_profile);
  83. }
  84. if (input_profile)
  85. qcms_profile_release(input_profile);
  86. if (rgb)
  87. qcms_profile_release(rgb);
  88. rgb = qcms_profile_from_path("sample-trunc.icc");
  89. if (rgb)
  90. qcms_profile_release(rgb);
  91. }
  92. static int current_counter;
  93. static int counter;
  94. void* malloc(size_t size)
  95. {
  96. if (counter == current_counter++) {
  97. return NULL;
  98. }
  99. static void* (*real_malloc)(size_t size);
  100. if (!real_malloc)
  101. real_malloc = (void* (*)(size_t)) dlsym(RTLD_NEXT, "malloc");
  102. return real_malloc(size);
  103. }
  104. void* calloc(size_t count, size_t size)
  105. {
  106. if (counter == current_counter++) {
  107. return NULL;
  108. }
  109. static void* (*real_calloc)(size_t count, size_t size);
  110. if (!real_calloc)
  111. real_calloc = (void* (*)(size_t, size_t)) dlsym(RTLD_NEXT, "calloc");
  112. return real_calloc(count, size);
  113. }
  114. const extern unsigned char gray_icc[];
  115. int main()
  116. {
  117. while (current_counter >= counter) {
  118. current_counter = 0;
  119. do_test();
  120. counter++;
  121. }
  122. return 0;
  123. }