krb5/store_stdio.c: workaround for solaris10/hpux/aix fread/fwrite duplication bug
authorRobert Manner <robert.manner@quest.com>
Tue, 29 Nov 2022 14:21:46 +0000 (15:21 +0100)
committerNico Williams <nico@cryptonector.com>
Mon, 9 Jan 2023 16:09:26 +0000 (10:09 -0600)
lib/krb5/store_stdio.c

index da7fcb57d1975d35cc56180370be01379f74bfb4..f12aeaf92d4690898a6d9f67c96ef44bdecaff0a 100644 (file)
@@ -83,6 +83,29 @@ stdio_store(krb5_storage * sp, const void *data, size_t size)
     ssize_t count;
     size_t rem = size;
 
+#if __sun || _AIX || __hpux
+    /* On solaris10, an fwrite following an fread causes the last character to be duplicated.
+        Example program to reproduce the issue:
+        #include <stdio.h>
+
+    int main() {
+            FILE *f = fopen("my", "w+");  // create a one byte file, containing 'A'
+            char a = 'A';
+            fwrite(&a, 1, 1, f);
+            fclose(f);
+
+            f = fopen("my", "r+");
+            char v;
+            fread(&v, 1, 1, f);    // Read the 'A' character
+            // fseeko(f, 0, SEEK_CUR); // -> this is a workaround, seek to where we are already
+            char b = 'B';
+            fwrite(&b, 1, 1, f);   // -> the file content becomes "AAB", despite we only write 'B'
+            fclose(f);
+            return 0;
+    }
+     */
+    fseeko(F(sp), 0, SEEK_CUR);
+#endif
     /* similar pattern to net_write() to support pipes */
     while (rem > 0) {
        count = fwrite(cbuf, 1, rem, F(sp));