{
const char *cmd = astman_get_header(m, "Command");
const char *id = astman_get_header(m, "ActionID");
- char *buf, *final_buf;
+ char *buf = NULL, *final_buf = NULL;
char template[] = "/tmp/ast-ami-XXXXXX"; /* template for temporary file */
int fd;
off_t l;
return 0;
}
- fd = mkstemp(template);
+ if ((fd = mkstemp(template)) < 0) {
+ ast_log(AST_LOG_WARNING, "Failed to create temporary file for command: %s\n", strerror(errno));
+ astman_send_error(s, m, "Command response construction error");
+ return 0;
+ }
astman_append(s, "Response: Follows\r\nPrivilege: Command\r\n");
if (!ast_strlen_zero(id)) {
}
/* FIXME: Wedge a ActionID response in here, waiting for later changes */
ast_cli_command(fd, cmd); /* XXX need to change this to use a FILE * */
- l = lseek(fd, 0, SEEK_END); /* how many chars available */
+ /* Determine number of characters available */
+ if ((l = lseek(fd, 0, SEEK_END)) < 0) {
+ ast_log(LOG_WARNING, "Failed to determine number of characters for command: %s\n", strerror(errno));
+ goto action_command_cleanup;
+ }
/* This has a potential to overflow the stack. Hence, use the heap. */
- buf = ast_calloc(1, l + 1);
- final_buf = ast_calloc(1, l + 1);
- if (buf) {
- lseek(fd, 0, SEEK_SET);
- if (read(fd, buf, l) < 0) {
- ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
- }
- buf[l] = '\0';
- if (final_buf) {
- term_strip(final_buf, buf, l);
- final_buf[l] = '\0';
- }
- astman_append(s, "%s", S_OR(final_buf, buf));
- ast_free(buf);
+ buf = ast_malloc(l + 1);
+ final_buf = ast_malloc(l + 1);
+
+ if (!buf || !final_buf) {
+ ast_log(LOG_WARNING, "Failed to allocate memory for temporary buffer\n");
+ goto action_command_cleanup;
+ }
+
+ if (lseek(fd, 0, SEEK_SET) < 0) {
+ ast_log(LOG_WARNING, "Failed to set position on temporary file for command: %s\n", strerror(errno));
+ goto action_command_cleanup;
}
+
+ if (read(fd, buf, l) < 0) {
+ ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
+ goto action_command_cleanup;
+ }
+
+ buf[l] = '\0';
+ term_strip(final_buf, buf, l);
+ final_buf[l] = '\0';
+ astman_append(s, "%s", final_buf);
+
+action_command_cleanup:
+
close(fd);
unlink(template);
astman_append(s, "--END COMMAND--\r\n\r\n");
- if (final_buf) {
- ast_free(final_buf);
- }
+
+ ast_free(buf);
+ ast_free(final_buf);
+
return 0;
}
fprintf(s->f, "%c", 0);
fflush(s->f);
- if ((l = ftell(s->f))) {
+ if ((l = ftell(s->f)) > 0) {
if (MAP_FAILED == (buf = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, s->fd, 0))) {
ast_log(LOG_WARNING, "mmap failed. Manager output was not processed\n");
} else {
if (!(format_list[i].bits & AST_FORMAT_AUDIO_MASK) || (format_list[i].bits == input_src)) {
continue;
}
+ /* Note that dst can never be -1, as an element of format_list will have
+ * at least one bit set. src cannot be -1 as well, as it is previously
+ * sanitized - hence it is safe to directly index tr_matrix with the results
+ * of powerof.
+ */
dst = powerof(format_list[i].bits);
src = powerof(input_src);
ast_str_reset(str);
format_t x;
format_t src_audio = src & AST_FORMAT_AUDIO_MASK;
format_t src_video = src & AST_FORMAT_VIDEO_MASK;
+ format_t x_bits;
/* if we don't have a source format, we just have to try all
possible destination formats */
return dest;
/* If we have a source audio format, get its format index */
- if (src_audio)
+ if (src_audio) {
src_audio = powerof(src_audio);
+ }
/* If we have a source video format, get its format index */
- if (src_video)
+ if (src_video) {
src_video = powerof(src_video);
+ }
+
+ /* Note that src_audio and src_video are guaranteed to not be
+ * negative at this point, as we ensured they were non-zero. It is
+ * safe to use the return value of powerof as an index into tr_matrix.
+ */
AST_RWLIST_RDLOCK(&translators);
continue;
/* if we don't have a translation path from the src
- to this format, remove it from the result */
- if (!tr_matrix[src_audio][powerof(x)].step) {
+ to this format, remove it from the result. Note that x_bits
+ cannot be less than 0 as x will always have one bit set to 1 */
+ x_bits = powerof(x);
+ if (!tr_matrix[src_audio][x_bits].step) {
res &= ~x;
continue;
}
/* now check the opposite direction */
- if (!tr_matrix[powerof(x)][src_audio].step)
+ if (!tr_matrix[x_bits][src_audio].step)
res &= ~x;
}
continue;
/* if we don't have a translation path from the src
- to this format, remove it from the result */
- if (!tr_matrix[src_video][powerof(x)].step) {
+ to this format, remove it from the result. Note that x_bits
+ cannot be less than 0 as x will always have one bit set to 1 */
+ x_bits = powerof(x);
+ if (!tr_matrix[src_video][x_bits].step) {
res &= ~x;
continue;
}
/* now check the opposite direction */
- if (!tr_matrix[powerof(x)][src_video].step)
+ if (!tr_matrix[x_bits][src_video].step)
res &= ~x;
}