From 56a32a8dbd3318e464021ac3bd2857254d0f26e3 Mon Sep 17 00:00:00 2001 From: "Roland C. Dowdeswell" Date: Wed, 20 Mar 2019 23:27:50 +0000 Subject: [PATCH] krb5_storage: normalise truncate behaviour w.r.t. file offset We choose a semantic for the file offset during truncate operations which is to leave the offset as is unless it is off the end of the file in which case we pull it back in to the end. We update fd and stdio handling to match the {,e}mem behaviour. --- lib/krb5/store_fd.c | 13 +++++++++++++ lib/krb5/store_stdio.c | 8 +++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/krb5/store_fd.c b/lib/krb5/store_fd.c index b1d41d0db..720ed66f7 100644 --- a/lib/krb5/store_fd.c +++ b/lib/krb5/store_fd.c @@ -97,8 +97,21 @@ fd_seek(krb5_storage * sp, off_t offset, int whence) static int fd_trunc(krb5_storage * sp, off_t offset) { + off_t tmpoff; + if (ftruncate(FD(sp), offset) == -1) return errno; + + tmpoff = lseek(FD(sp), 0, SEEK_CUR); + if (tmpoff == -1) + return errno; + + if (tmpoff > offset) { + tmpoff = lseek(FD(sp), offset, SEEK_SET); + if (tmpoff == -1) + return errno; + } + return 0; } diff --git a/lib/krb5/store_stdio.c b/lib/krb5/store_stdio.c index 76f7710e5..99cadf94c 100644 --- a/lib/krb5/store_stdio.c +++ b/lib/krb5/store_stdio.c @@ -108,14 +108,20 @@ stdio_seek(krb5_storage * sp, off_t offset, int whence) static int stdio_trunc(krb5_storage * sp, off_t offset) { + off_t tmpoff; int save_errno = errno; if (fflush(F(sp)) == EOF) return errno; + tmpoff = ftello(F(sp)); + if (tmpoff > offset) + tmpoff = offset; if (ftruncate(fileno(F(sp)), offset) == -1) return errno; - if (fseeko(F(sp), offset, SEEK_SET) == -1) + if (fseeko(F(sp), 0, SEEK_END) == -1) return errno; + if (fseeko(F(sp), tmpoff, SEEK_SET) == -1) + return errno; errno = save_errno; return 0; }