1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- /* shttpd - keeping track of a stream's resources
- Copyright (C) 2018 Ariadne Devos
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>. */
- #include <stddef.h>
- #include <sHT/compiler.h>
- #include <sHT/paper.h>
- #include <sHT/resource.h>
- #include <sHT/stream.h>
- _Bool
- sHT_init_stream(struct sHT_object_cache *papers, struct sHT_stream *stream)
- {
- sHT_paper write_paper, read_paper;
- write_paper = sHT_alloc(papers);
- if (sHT_unlikely(write_paper == NULL))
- goto enomem;
- read_paper = sHT_alloc(papers);
- if (sHT_unlikely(read_paper == NULL))
- goto write_paper;
- *stream = (struct sHT_stream) {
- /* Try to provoke crashes when @code{stream->fd} is used before
- initialisation. No one uses -2 for invalid file descriptors,
- sHT uses it for undefined file descriptors (not the same
- thing). */
- .fd = -2,
- .flags = 0,
- .to_write = {
- .first = write_paper,
- .offset = 0,
- .length = 0,
- },
- .has_read = {
- .first = read_paper,
- .offset = 0,
- .length = 0,
- },
- };
- sHT_depend(stream, write_paper);
- sHT_depend(stream, read_paper);
- _Bool ret = 0;
- sHT_depend(ret, stream);
- return ret;
- write_paper:
- /* write_paper may speculatively be NULL, but sHT_free allows that. */
- sHT_free(papers, write_paper);
- enomem:
- /* TODO: compile this out on release builds */
- /* Try to provoke a crash when @var{stream} is used, even though it is
- uninitialised. */
- /* No one uses -2 for invalid file descriptors, sHT uses it for
- undefined file descriptors (not the same thing). */
- stream->fd = -2;
- /* NULL papers in buffers of initialised structures are always
- incorrect (in sHT, not necessarily elsewhere). So we can try to
- provoke a segfault. */
- stream->has_read = stream->to_write = (struct sHT_buffer) {
- .first = NULL,
- .offset = 0,
- .length = sHT_PAPER_SIZE,
- };
- /* TODO: inform Valgrind of all this undefinedness with
- VALGRIND_MAKE_MEM_UNDEFINED. */
- return 1;
- }
- void
- sHT_free_stream(struct sHT_object_cache *papers, struct sHT_stream *stream)
- {
- sHT_free(papers, stream->has_read.first);
- sHT_free(papers, stream->to_write.first);
- /* TODO: consider tainting @var{stream}'s buffers, see
- @var{sHT_init_stream}. */
- }
|