| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2012-2013 Vojtech Horky
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * @file
|
|---|
| 9 | * Formatting and processing of failed assertion messages.
|
|---|
| 10 | *
|
|---|
| 11 | * We are using static buffers to prevent any calls to malloc()/free()
|
|---|
| 12 | * by the testing framework.
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | /*
|
|---|
| 16 | * We need _BSD_SOURCE because of vsnprintf() when compiling under C89.
|
|---|
| 17 | * In newer versions of features.h, _DEFAULT_SOURCE must be defined as well.
|
|---|
| 18 | */
|
|---|
| 19 | #define _BSD_SOURCE
|
|---|
| 20 | #define _DEFAULT_SOURCE
|
|---|
| 21 |
|
|---|
| 22 | #include "internal.h"
|
|---|
| 23 |
|
|---|
| 24 | #pragma warning(push, 0)
|
|---|
| 25 | #include <setjmp.h>
|
|---|
| 26 | #include <stdarg.h>
|
|---|
| 27 | #include <stdio.h>
|
|---|
| 28 | #pragma warning(pop)
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | /** Maximum length of failed-assert message. */
|
|---|
| 32 | #define MAX_MESSAGE_LENGTH 256
|
|---|
| 33 |
|
|---|
| 34 | /** How many assertion messages we need to keep in memory at once. */
|
|---|
| 35 | #define MESSAGE_BUFFER_COUNT 2
|
|---|
| 36 |
|
|---|
| 37 | /** Assertion message buffers. */
|
|---|
| 38 | static char message_buffer[MESSAGE_BUFFER_COUNT][MAX_MESSAGE_LENGTH + 1];
|
|---|
| 39 |
|
|---|
| 40 | /** Currently active assertion buffer. */
|
|---|
| 41 | static int message_buffer_index = 0;
|
|---|
| 42 |
|
|---|
| 43 | void pcut_failed_assertion_fmt(const char *filename, int line, const char *fmt, ...) {
|
|---|
| 44 | va_list args;
|
|---|
| 45 | char *current_buffer = message_buffer[message_buffer_index];
|
|---|
| 46 | size_t offset = 0;
|
|---|
| 47 | message_buffer_index = (message_buffer_index + 1) % MESSAGE_BUFFER_COUNT;
|
|---|
| 48 |
|
|---|
| 49 | pcut_snprintf(current_buffer, MAX_MESSAGE_LENGTH, "%s:%d: ", filename, line);
|
|---|
| 50 | offset = pcut_str_size(current_buffer);
|
|---|
| 51 |
|
|---|
| 52 | if (offset + 1 < MAX_MESSAGE_LENGTH) {
|
|---|
| 53 | va_start(args, fmt);
|
|---|
| 54 | vsnprintf(current_buffer + offset, MAX_MESSAGE_LENGTH - offset, fmt, args);
|
|---|
| 55 | va_end(args);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | pcut_failed_assertion(current_buffer);
|
|---|
| 59 | }
|
|---|