From: Andrea Cervesato <andrea.cervesato@suse.de>
To: Linux Test Project <ltp@lists.linux.it>
Subject: [LTP] [PATCH v2 1/6] Add safe macros for stream testing suite
Date: Wed, 04 Mar 2026 14:25:39 +0100 [thread overview]
Message-ID: <20260304-stream_refactoring-v2-1-3e48ada8ff1a@suse.com> (raw)
In-Reply-To: <20260304-stream_refactoring-v2-0-3e48ada8ff1a@suse.com>
From: Andrea Cervesato <andrea.cervesato@suse.com>
Introduce the following SAFE_* macros for stream file testing:
- SAFE_FREAD
- SAFE_FWRITE
- SAFE_FREOPEN
- SAFE_FSEEK
- SAFE_FTELL
- SAFE_FILENO
- SAFE_FFLUSH
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
include/safe_stdio_fn.h | 21 ++++++++++
include/tst_safe_stdio.h | 21 ++++++++++
lib/safe_stdio.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 143 insertions(+)
diff --git a/include/safe_stdio_fn.h b/include/safe_stdio_fn.h
index 3818a86571a6d9bc63fcf432c93683bb3298e5b2..255a85949749748533e93b86c7cf170900e95082 100644
--- a/include/safe_stdio_fn.h
+++ b/include/safe_stdio_fn.h
@@ -32,4 +32,25 @@ int safe_asprintf(const char *file, const int lineno, void (cleanup_fn)(void),
FILE *safe_popen(const char *file, const int lineno, void (cleanup_fn)(void),
const char *command, const char *type);
+size_t safe_fread(const char *file, const int lineno,
+ void *ptr, size_t size, size_t n, FILE *stream);
+
+size_t safe_fwrite(const char *file, const int lineno,
+ const void *ptr, size_t size, size_t n, FILE *stream);
+
+FILE *safe_freopen(const char *file, const int lineno,
+ const char *path, const char *mode, FILE *stream);
+
+int safe_fseek(const char *file, const int lineno,
+ FILE *f, long offset, int whence);
+
+long safe_ftell(const char *file, const int lineno,
+ FILE *f);
+
+int safe_fileno(const char *file, const int lineno,
+ FILE *stream);
+
+int safe_fflush(const char *file, const int lineno,
+ FILE *stream);
+
#endif /* SAFE_STDIO_FN_H__ */
diff --git a/include/tst_safe_stdio.h b/include/tst_safe_stdio.h
index e4bff34da15c9116809fcf851cbf544a51e384ef..45d4bc7e5d590468eec7a95b0aa37019e4e15f18 100644
--- a/include/tst_safe_stdio.h
+++ b/include/tst_safe_stdio.h
@@ -21,4 +21,25 @@
#define SAFE_POPEN(command, type) \
safe_popen(__FILE__, __LINE__, NULL, command, type)
+#define SAFE_FREAD(ptr, size, n, stream) \
+ safe_fread(__FILE__, __LINE__, ptr, size, n, stream)
+
+#define SAFE_FWRITE(ptr, size, n, stream) \
+ safe_fwrite(__FILE__, __LINE__, ptr, size, n, stream)
+
+#define SAFE_FREOPEN(path, mode, stream) \
+ safe_freopen(__FILE__, __LINE__, path, mode, stream)
+
+#define SAFE_FSEEK(f, offset, whence) \
+ safe_fseek(__FILE__, __LINE__, f, offset, whence)
+
+#define SAFE_FTELL(f) \
+ safe_ftell(__FILE__, __LINE__, f)
+
+#define SAFE_FILENO(f) \
+ safe_fileno(__FILE__, __LINE__, f)
+
+#define SAFE_FFLUSH(f) \
+ safe_fflush(__FILE__, __LINE__, f)
+
#endif /* TST_SAFE_STDIO_H__ */
diff --git a/lib/safe_stdio.c b/lib/safe_stdio.c
index ab23e43bb0835cdca5eaa015bc873fd23f9a8408..feb8a4b5c87c8b2cb7f7f59f6ba763e45acdf237 100644
--- a/lib/safe_stdio.c
+++ b/lib/safe_stdio.c
@@ -99,3 +99,104 @@ FILE *safe_popen(const char *file, const int lineno, void (cleanup_fn)(void),
return stream;
}
+
+size_t safe_fread(const char *file, const int lineno,
+ void *ptr, size_t size, size_t n, FILE *stream)
+{
+ size_t ret;
+
+ ret = fread(ptr, size, n, stream);
+ if (ret != n) {
+ tst_brkm_(file, lineno, TBROK, NULL,
+ "fread(%p, %lu, %lu, %p) read %lu bytes",
+ ptr, size, n, stream, ret);
+ }
+
+ return ret;
+}
+
+size_t safe_fwrite(const char *file, const int lineno,
+ const void *ptr, size_t size, size_t n, FILE *stream)
+{
+ size_t ret;
+
+ ret = fwrite(ptr, size, n, stream);
+ if (ret != n) {
+ tst_brkm_(file, lineno, TBROK, NULL,
+ "fwrite(%p, %lu, %lu, %p) written %lu bytes",
+ ptr, size, n, stream, ret);
+ }
+
+ return ret;
+}
+
+FILE *safe_freopen(const char *file, const int lineno,
+ const char *path, const char *mode, FILE *stream)
+{
+ FILE *f = freopen(path, mode, stream);
+
+ if (!f) {
+ tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
+ "freopen(%s,%s,%p) failed", path, mode, stream);
+ }
+
+ return f;
+}
+
+int safe_fseek(const char *file, const int lineno,
+ FILE *f, long offset, int whence)
+{
+ int ret;
+
+ errno = 0;
+ ret = fseek(f, offset, whence);
+
+ if (ret == -1) {
+ tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
+ "fseek(%p, %ld, %d)", f, offset, whence);
+ }
+
+ return ret;
+}
+
+long safe_ftell(const char *file, const int lineno,
+ FILE *f)
+{
+ long ret;
+
+ errno = 0;
+ ret = ftell(f);
+
+ if (ret == -1)
+ tst_brkm_(file, lineno, TBROK | TERRNO, NULL, "ftell(%p)", f);
+
+ return ret;
+}
+
+int safe_fileno(const char *file, const int lineno,
+ FILE *f)
+{
+ int ret;
+
+ errno = 0;
+ ret = fileno(f);
+
+ if (ret == -1)
+ tst_brkm_(file, lineno, TBROK | TERRNO, NULL, "fileno(%p)", f);
+
+ return ret;
+}
+
+int safe_fflush(const char *file, const int lineno,
+ FILE *f)
+{
+ int ret;
+
+ errno = 0;
+ ret = fflush(f);
+
+ if (ret == EOF)
+ tst_brkm_(file, lineno, TBROK | TERRNO, NULL, "fflush(%p)", f);
+
+ return ret;
+}
--
2.51.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2026-03-04 13:27 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-04 13:25 [LTP] [PATCH v2 0/6] Rewrite fs stream testing suite Andrea Cervesato
2026-03-04 13:25 ` Andrea Cervesato [this message]
2026-03-04 13:25 ` [LTP] [PATCH v2 2/6] fs: rewrite stream01 test using new API Andrea Cervesato
2026-04-07 14:57 ` Cyril Hrubis
2026-03-04 13:25 ` [LTP] [PATCH v2 3/6] fs: rewrite stream02 " Andrea Cervesato
2026-04-07 14:59 ` Cyril Hrubis
2026-03-04 13:25 ` [LTP] [PATCH v2 4/6] fs: rewrite stream03 " Andrea Cervesato
2026-03-04 13:25 ` [LTP] [PATCH v2 5/6] fs: rewrite stream04 " Andrea Cervesato
2026-03-04 13:25 ` [LTP] [PATCH v2 6/6] fs: rewrite stream05 " Andrea Cervesato
2026-04-07 15:30 ` Cyril Hrubis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260304-stream_refactoring-v2-1-3e48ada8ff1a@suse.com \
--to=andrea.cervesato@suse.de \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox