mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-05-18 11:35:15 -04:00
Upgrade sendfile7 to not mash size on stack. offset is a nullptr most of the time, don't dereference it. When falling back to read/write, just read in chunks of 4096, because I don't know how to make llvm use c99 dynamic arrays. Check for HAVE_SENDFILE. Fixes #3505
This commit is contained in:
@@ -1,46 +1,57 @@
|
||||
#ifndef ZM_SENDFILE_H
|
||||
#define ZM_SENDFILE_H
|
||||
|
||||
#ifdef HAVE_SENDFILE4_SUPPORT
|
||||
#if defined(HAVE_SENDFILE) && defined(HAVE_SENDFILE4_SUPPORT)
|
||||
#include <sys/sendfile.h>
|
||||
#elif HAVE_SENDFILE7_SUPPORT
|
||||
#elif defined(HAVE_SENDFILE) && defined(HAVE_SENDFILE7_SUPPORT)
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/uio.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
/* Function to send the contents of a file. Will use sendfile or fall back to reading/writing */
|
||||
|
||||
ssize_t zm_sendfile(int out_fd, int in_fd, off_t *offset, ssize_t size) {
|
||||
#ifdef HAVE_SENDFILE4_SUPPORT
|
||||
ssize_t zm_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) {
|
||||
#if defined(HAVE_SENDFILE) && defined(HAVE_SENDFILE4_SUPPORT)
|
||||
ssize_t err = sendfile(out_fd, in_fd, offset, size);
|
||||
if (err < 0) {
|
||||
return -errno;
|
||||
}
|
||||
return err;
|
||||
|
||||
#elif HAVE_SENDFILE7_SUPPORT
|
||||
off_t sbytes = 0;
|
||||
off_t ofs = offset ? *offset : 0;
|
||||
ssize_t err = sendfile(in_fd, out_fd, ofs, size, nullptr, &sbytes, 0);
|
||||
#elif defined(HAVE_SENDFILE) && defined(HAVE_SENDFILE7_SUPPORT)
|
||||
off_t sbytes;
|
||||
ssize_t err = sendfile(in_fd, out_fd, (offset ? *offset: 0), size, nullptr, &sbytes, 0);
|
||||
if (err && errno != EAGAIN)
|
||||
return -errno;
|
||||
return sbytes;
|
||||
#else
|
||||
uint8_t buffer[size];
|
||||
ssize_t err = read(in_fd, buffer, size);
|
||||
uint8_t buffer[4096];
|
||||
size_t chunk_size = (size > 4096 ? 4096 : size);
|
||||
|
||||
ssize_t err = read(in_fd, buffer, chunk_size);
|
||||
if (err < 0) {
|
||||
Error("Unable to read %zu bytes: %s", size, strerror(errno));
|
||||
Error("Unable to read %zu bytes of %zu: %s", chunk_size, size, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
if (!err) {
|
||||
Error("Got EOF despite wanting to read %zu bytes", 4096);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = write(out_fd, buffer, size);
|
||||
chunk_size = err;
|
||||
|
||||
err = write(out_fd, buffer, chunk_size);
|
||||
if (err < 0) {
|
||||
Error("Unable to write %zu bytes: %s", size, strerror(errno));
|
||||
Error("Unable to write %zu bytes: %s", chunk_size, strerror(errno));
|
||||
return -errno;
|
||||
} else if (err != chunk_size) {
|
||||
Debug(1, "Sent less than desired %zu < %zu", err, chunk_size);
|
||||
}
|
||||
|
||||
return err;
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user