properly deal with errno on short read/write
authorJohn Kohl <jtkohl@mit.edu>
Tue, 12 Feb 1991 14:03:41 +0000 (14:03 +0000)
committerJohn Kohl <jtkohl@mit.edu>
Tue, 12 Feb 1991 14:03:41 +0000 (14:03 +0000)
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@1669 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/krb5/ccache/stdio/scc_read.c
src/lib/krb5/ccache/stdio/scc_write.c

index 790a3779f27e026c8636154024b9dfe941f84996..62515dc030693de65df1b04e7acf89566e986686 100644 (file)
@@ -36,8 +36,9 @@ krb5_scc_read(id, buf, len)
 {
      int ret;
 
+     errno = 0;
      ret = fread((char *) buf, 1, len, ((krb5_scc_data *) id->data)->file);
-     if (ret == 0)
+     if ((ret == 0) && errno)
          return krb5_scc_interpret(errno);
      else if (ret != len)
          return KRB5_CC_END;
@@ -166,11 +167,12 @@ krb5_scc_read_keyblock(id, keyblock)
      if (keyblock->contents == NULL)
          return KRB5_CC_NOMEM;
      
+     errno = 0;
      ret = fread((char *)keyblock->contents, 1,
                 (keyblock->length)*sizeof(krb5_octet),
                 ((krb5_scc_data *) id->data)->file);
 
-     if (ret == 0) {
+     if ((ret == 0) && errno) {
         xfree(keyblock->contents);
         return krb5_scc_interpret(errno);
      }
@@ -203,9 +205,10 @@ krb5_scc_read_data(id, data)
      if (data->data == NULL)
          return KRB5_CC_NOMEM;
 
+     errno = 0;
      ret = fread((char *)data->data, 1,
                 data->length, ((krb5_scc_data *) id->data)->file);
-     if (ret == -1) {
+     if ((ret == 0) && errno) {
         xfree(data->data);
         return krb5_scc_interpret(errno);
      }
@@ -240,9 +243,10 @@ krb5_scc_read_addr(id, addr)
      if (addr->contents == NULL)
          return KRB5_CC_NOMEM;
 
+     errno = 0;
      ret = fread((char *)addr->contents, 1, (addr->length)*sizeof(krb5_octet),
                 ((krb5_scc_data *) id->data)->file);
-     if (ret == -1) {
+     if ((ret == 0) && errno) {
          xfree(addr->contents);
          return krb5_scc_interpret(errno);
      }
@@ -373,10 +377,11 @@ krb5_scc_read_authdatum(id, a)
     a->contents = (krb5_octet *) malloc(a->length);
     if (a->contents == NULL)
         return KRB5_CC_NOMEM;
+    errno = 0;
     ret = fread ((char *)a->contents, 1,
                 (a->length)*sizeof(krb5_octet),
                 ((krb5_scc_data *) id->data)->file);
-    if (ret == 0) {
+    if ((ret == 0) && errno) {
        xfree(a->contents);
        return krb5_scc_interpret(errno);
     }
index e180437cc6defb0714e41210d72940aff950e009..78409be4ecc9c26aa83f6854de6f83ec83d92019 100644 (file)
@@ -38,10 +38,12 @@ krb5_scc_write(id, buf, len)
 {
      int ret;
 
+     errno = 0;
      ret = fwrite((char *) buf, 1, len, ((krb5_scc_data *)id->data)->file);
-     if (ret == 0) {
+     if ((ret == 0) && errno) {
          return krb5_scc_interpret (errno);
-     }
+     } else if (ret != len)
+        return KRB5_CC_END;
      return KRB5_OK;
 }
 
@@ -118,10 +120,11 @@ krb5_scc_store_keyblock(id, keyblock)
      CHECK(ret);
      ret = krb5_scc_store_int(id, &keyblock->length);
      CHECK(ret);
+     errno = 0;
      ret = fwrite((char *)keyblock->contents, 1,
                  (keyblock->length)*sizeof(krb5_octet),
                  ((krb5_scc_data *) id->data)->file);
-     if (ret == 0)
+     if ((ret == 0) && errno)
          return krb5_scc_interpret(errno);
      if (ret != (keyblock->length)*sizeof(krb5_octet))
         return KRB5_CC_END;
@@ -140,10 +143,11 @@ krb5_scc_store_addr(id, addr)
      CHECK(ret);
      ret = krb5_scc_store_int(id, &addr->length);
      CHECK(ret);
+     errno = 0;
      ret = fwrite((char *)addr->contents, 1,
                  (addr->length)*sizeof(krb5_octet),
                  ((krb5_scc_data *) id->data)->file);
-     if (ret == 0)
+     if ((ret == 0) && errno)
          return krb5_scc_interpret(errno);
      if (ret != (addr->length)*sizeof(krb5_octet))
         return KRB5_CC_END;
@@ -160,11 +164,13 @@ krb5_scc_store_data(id, data)
 
      ret = krb5_scc_store_int32(id, &data->length);
      CHECK(ret);
+     errno = 0;
      ret = fwrite(data->data, 1, data->length,
                  ((krb5_scc_data *) id->data)->file);
-     if (ret == 0)
+     if ((ret == 0) && errno)
          return krb5_scc_interpret(errno);
-
+     else if (ret != data->length)
+        return KRB5_CC_END;
      return KRB5_OK;
 }
 
@@ -257,5 +263,5 @@ krb5_scc_store_authdatum (id, a)
     CHECK(ret);
     ret = krb5_scc_store_int32(id, &a->length);
     CHECK(ret);
-    return krb5_scc_write(id, a->contents, a->length);
+    return krb5_scc_write(id, (krb5_pointer) a->contents, a->length);
 }