stream.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* shttpd - keeping track of a stream's resources
  2. Copyright (C) 2018 Ariadne Devos
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <stddef.h>
  14. #include <sHT/compiler.h>
  15. #include <sHT/paper.h>
  16. #include <sHT/resource.h>
  17. #include <sHT/stream.h>
  18. _Bool
  19. sHT_init_stream(struct sHT_object_cache *papers, struct sHT_stream *stream)
  20. {
  21. sHT_paper write_paper, read_paper;
  22. write_paper = sHT_alloc(papers);
  23. if (sHT_unlikely(write_paper == NULL))
  24. goto enomem;
  25. read_paper = sHT_alloc(papers);
  26. if (sHT_unlikely(read_paper == NULL))
  27. goto write_paper;
  28. *stream = (struct sHT_stream) {
  29. /* Try to provoke crashes when @code{stream->fd} is used before
  30. initialisation. No one uses -2 for invalid file descriptors,
  31. sHT uses it for undefined file descriptors (not the same
  32. thing). */
  33. .fd = -2,
  34. .flags = 0,
  35. .to_write = {
  36. .first = write_paper,
  37. .offset = 0,
  38. .length = 0,
  39. },
  40. .has_read = {
  41. .first = read_paper,
  42. .offset = 0,
  43. .length = 0,
  44. },
  45. };
  46. sHT_depend(stream, write_paper);
  47. sHT_depend(stream, read_paper);
  48. _Bool ret = 0;
  49. sHT_depend(ret, stream);
  50. return ret;
  51. write_paper:
  52. /* write_paper may speculatively be NULL, but sHT_free allows that. */
  53. sHT_free(papers, write_paper);
  54. enomem:
  55. /* TODO: compile this out on release builds */
  56. /* Try to provoke a crash when @var{stream} is used, even though it is
  57. uninitialised. */
  58. /* No one uses -2 for invalid file descriptors, sHT uses it for
  59. undefined file descriptors (not the same thing). */
  60. stream->fd = -2;
  61. /* NULL papers in buffers of initialised structures are always
  62. incorrect (in sHT, not necessarily elsewhere). So we can try to
  63. provoke a segfault. */
  64. stream->has_read = stream->to_write = (struct sHT_buffer) {
  65. .first = NULL,
  66. .offset = 0,
  67. .length = sHT_PAPER_SIZE,
  68. };
  69. /* TODO: inform Valgrind of all this undefinedness with
  70. VALGRIND_MAKE_MEM_UNDEFINED. */
  71. return 1;
  72. }
  73. void
  74. sHT_free_stream(struct sHT_object_cache *papers, struct sHT_stream *stream)
  75. {
  76. sHT_free(papers, stream->has_read.first);
  77. sHT_free(papers, stream->to_write.first);
  78. /* TODO: consider tainting @var{stream}'s buffers, see
  79. @var{sHT_init_stream}. */
  80. }