diff -ur krb5-1.2.7/src/appl/telnet/libtelnet/kerberos5.c krb5-1.2.7/src/appl/telnet/libtelnet/kerberos5.c
--- krb5-1.2.7/src/appl/telnet/libtelnet/kerberos5.c	2002-03-29 00:07:09.000000000 -0500
+++ krb5-1.2.7/src/appl/telnet/libtelnet/kerberos5.c	2003-02-03 17:30:18.000000000 -0500
@@ -441,6 +441,10 @@
 		 * first component of a service name especially since
 		 * the default is of length 4.
 		 */
+		if (krb5_princ_size(telnet_context,ticket->server) < 1) {
+		    (void) strcpy(errbuf, "malformed service name");
+		    goto errout;
+		}
 		if (krb5_princ_component(telnet_context,ticket->server,0)->length < 256) {
 		    char princ[256];
 		    strncpy(princ,	
diff -ur krb5-1.2.7/src/clients/ksu/heuristic.c krb5-1.2.7/src/clients/ksu/heuristic.c
--- krb5-1.2.7/src/clients/ksu/heuristic.c	2003-02-03 15:24:57.000000000 -0500
+++ krb5-1.2.7/src/clients/ksu/heuristic.c	2003-02-03 17:56:38.000000000 -0500
@@ -355,7 +355,7 @@
 		krb5_data *p2 =
 		    krb5_princ_component(context, temp_client, j);
 
-		if ((p1->length != p2->length) ||
+		if (!p1 || !p2 || (p1->length != p2->length) ||
 		    memcmp(p1->data,p2->data,p1->length)){
 		    got_one = FALSE;
 		    break;
diff -ur krb5-1.2.7/src/clients/ksu/krb_auth_su.c krb5-1.2.7/src/clients/ksu/krb_auth_su.c
--- krb5-1.2.7/src/clients/ksu/krb_auth_su.c	1999-12-06 16:56:09.000000000 -0500
+++ krb5-1.2.7/src/clients/ksu/krb_auth_su.c	2003-02-03 17:57:22.000000000 -0500
@@ -620,7 +620,9 @@
                   	      krb5_princ_realm(context, temp_client)->length))){
 
 
-			if(nelem){ 
+			if(nelem &&
+			   (krb5_princ_size(context, *client) > 0) &&
+			   (krb5_princ_size(context, temp_client) > 0)){ 
 				krb5_data *p1 =
 				 krb5_princ_component(context, *client, 0);
         			krb5_data *p2 = 
diff -ur krb5-1.2.7/src/include/krb5.hin krb5-1.2.7/src/include/krb5.hin
--- krb5-1.2.7/src/include/krb5.hin	2002-04-16 19:47:53.000000000 -0400
+++ krb5-1.2.7/src/include/krb5.hin	2003-02-03 17:09:14.000000000 -0500
@@ -326,7 +326,7 @@
 #define	krb5_princ_size(context, princ) (princ)->length
 #define	krb5_princ_type(context, princ) (princ)->type
 #define	krb5_princ_name(context, princ) (princ)->data
-#define	krb5_princ_component(context, princ,i) ((princ)->data + i)
+#define	krb5_princ_component(context, princ,i) (i < krb5_princ_size(context, princ) ? ((princ)->data + i) : NULL)
 
 /*
  * end "base-defs.h"
diff -ur krb5-1.2.7/src/kdc/do_tgs_req.c krb5-1.2.7/src/kdc/do_tgs_req.c
--- krb5-1.2.7/src/kdc/do_tgs_req.c	2003-02-03 15:24:58.000000000 -0500
+++ krb5-1.2.7/src/kdc/do_tgs_req.c	2003-02-03 17:54:27.000000000 -0500
@@ -180,7 +180,7 @@
 		krb5_data *tgs_1 =
 		    krb5_princ_component(kdc_context, tgs_server, 1);
 
-		if (server_1->length != tgs_1->length ||
+		if (!tgs_1 || server_1->length != tgs_1->length ||
 		    memcmp(server_1->data, tgs_1->data, tgs_1->length)) {
 		    krb5_db_free_principal(kdc_context, &server, nprincs);
 		    find_alternate_tgs(request, &server, &more, &nprincs);
diff -ur krb5-1.2.7/src/kdc/kdc_util.c krb5-1.2.7/src/kdc/kdc_util.c
--- krb5-1.2.7/src/kdc/kdc_util.c	2003-02-03 15:24:58.000000000 -0500
+++ krb5-1.2.7/src/kdc/kdc_util.c	2003-02-03 17:56:05.000000000 -0500
@@ -157,7 +157,8 @@
 krb5_boolean krb5_is_tgs_principal(principal)
 	krb5_principal	principal;
 {
-	if ((krb5_princ_component(kdc_context, principal, 0)->length ==
+	if ((krb5_princ_size(kdc_context, principal) > 0) &&
+	    (krb5_princ_component(kdc_context, principal, 0)->length ==
 	     KRB5_TGS_NAME_SIZE) &&
 	    (!memcmp(krb5_princ_component(kdc_context, principal, 0)->data,
 		     KRB5_TGS_NAME, KRB5_TGS_NAME_SIZE)))
@@ -1195,7 +1196,8 @@
 	    return KRB_AP_ERR_NOT_US;
 	}
 	/* ...and that the second component matches the server realm... */
-	if ((krb5_princ_component(kdc_context, ticket->server, 1)->length !=
+	if ((krb5_princ_size(kdc_context, ticket->server) <= 1) ||
+	    (krb5_princ_component(kdc_context, ticket->server, 1)->length !=
 	     krb5_princ_realm(kdc_context, request->server)->length) ||
 	    memcmp(krb5_princ_component(kdc_context, ticket->server, 1)->data,
 		   krb5_princ_realm(kdc_context, request->server)->data,
diff -ur krb5-1.2.7/src/krb524/krb524d.c krb5-1.2.7/src/krb524/krb524d.c
--- krb5-1.2.7/src/krb524/krb524d.c	2002-08-29 02:48:05.000000000 -0400
+++ krb5-1.2.7/src/krb524/krb524d.c	2003-02-03 17:33:13.000000000 -0500
@@ -317,7 +317,7 @@
      if (debug)
 	  printf("V5 ticket decoded\n");
      
-     if( v5tkt->server->length >= 1
+     if( krb5_princ_size(context, v5tkt->server) >= 1
 	 &&krb5_princ_component(context, v5tkt->server, 0)->length == 3
 	 &&strncmp(krb5_princ_component(context, v5tkt->server, 0)->data,
 		   "afs", 3) == 0) {
diff -ur krb5-1.2.7/src/lib/krb5/keytab/file/ktf_util.c krb5-1.2.7/src/lib/krb5/keytab/file/ktf_util.c
--- krb5-1.2.7/src/lib/krb5/keytab/file/ktf_util.c	1999-09-24 17:19:01.000000000 -0400
+++ krb5-1.2.7/src/lib/krb5/keytab/file/ktf_util.c	2003-02-03 18:02:25.000000000 -0500
@@ -441,7 +441,7 @@
     return 0;
 fail:
     
-    for (i = 0; i < ret_entry->principal->length; i++) {
+    for (i = 0; i < krb5_princ_size(context, ret_entry->principal); i++) {
 	    princ = krb5_princ_component(context, ret_entry->principal, i);
 	    if (princ->data)
 		    free(princ->data);
@@ -498,9 +498,9 @@
     }
 
     if (KTVERSION(id) == KRB5_KT_VNO_1) {
-	    count = (krb5_int16) entry->principal->length + 1;
+	    count = (krb5_int16) krb5_princ_size(context, entry->principal) + 1;
     } else {
-	    count = htons((u_short) entry->principal->length);
+	    count = htons((u_short) krb5_princ_size(context, entry->principal));
     }
     
     if (!xfwrite(&count, sizeof(count), 1, KTFILEP(id))) {
@@ -519,7 +519,7 @@
 	    goto abend;
     }
 
-    count = (krb5_int16) entry->principal->length;
+    count = (krb5_int16) krb5_princ_size(context, entry->principal);
     for (i = 0; i < count; i++) {
 	princ = krb5_princ_component(context, entry->principal, i);
 	size = princ->length;
@@ -620,7 +620,7 @@
     krb5_int32 total_size, i;
     krb5_error_code retval = 0;
 
-    count = (krb5_int16) entry->principal->length;
+    count = (krb5_int16) krb5_princ_size(context, entry->principal);
         
     total_size = sizeof(count);
     total_size += krb5_princ_realm(context, entry->principal)->length + (sizeof(krb5_int16));
diff -ur krb5-1.2.7/src/lib/krb5/krb/gc_frm_kdc.c krb5-1.2.7/src/lib/krb5/krb/gc_frm_kdc.c
--- krb5-1.2.7/src/lib/krb5/krb/gc_frm_kdc.c	1999-09-24 17:19:24.000000000 -0400
+++ krb5-1.2.7/src/lib/krb5/krb/gc_frm_kdc.c	2003-02-03 17:35:40.000000000 -0500
@@ -347,7 +347,9 @@
 	for (next_server = top_server; *next_server; next_server++) {
             krb5_data *realm_1 = krb5_princ_component(context, next_server[0], 1);
             krb5_data *realm_2 = krb5_princ_component(context, tgtr->server, 1);
-            if (realm_1->length == realm_2->length &&
+	    if (realm_1 != NULL &&
+		realm_2 != NULL &&
+                realm_1->length == realm_2->length &&
                 !memcmp(realm_1->data, realm_2->data, realm_1->length)) {
 		break;
             }
diff -ur krb5-1.2.7/src/lib/krb5/krb/parse.c krb5-1.2.7/src/lib/krb5/krb/parse.c
--- krb5-1.2.7/src/lib/krb5/krb/parse.c	2002-02-28 12:08:35.000000000 -0500
+++ krb5-1.2.7/src/lib/krb5/krb/parse.c	2003-02-03 17:44:04.000000000 -0500
@@ -173,11 +173,13 @@
 				cp++;
 				size++;
 			} else if (c == COMPONENT_SEP) {
-				krb5_princ_component(context, principal, i)->length = size;
+				if (krb5_princ_size(context, principal) > i)
+					krb5_princ_component(context, principal, i)->length = size;
 				size = 0;
 				i++;
 			} else if (c == REALM_SEP) {
-				krb5_princ_component(context, principal, i)->length = size;
+				if (krb5_princ_size(context, principal) > i)
+					krb5_princ_component(context, principal, i)->length = size;
 				size = 0;
 				parsed_realm = cp+1;
 			} else
@@ -186,7 +188,8 @@
 		if (parsed_realm)
 			krb5_princ_realm(context, principal)->length = size;
 		else
-			krb5_princ_component(context, principal, i)->length = size;
+			if (krb5_princ_size(context, principal) > i)
+				krb5_princ_component(context, principal, i)->length = size;
 		if (i + 1 != components) {
 #if !defined(_MSDOS) && !defined(_WIN32) && !defined(macintosh)
 			fprintf(stderr,
--- krb5-1.2.7/src/lib/krb5/krb/srv_rcache.c	1999-09-24 17:19:48.000000000 -0400
+++ krb5-1.2.7/src/lib/krb5/krb/srv_rcache.c	2003-02-03 19:29:32.000000000 -0500
@@ -48,6 +48,9 @@
     unsigned long uid = geteuid();
 #endif
     
+    if (piece == NULL)
+	return ENOMEM;
+    
     rcache = (krb5_rcache) malloc(sizeof(*rcache));
     if (!rcache)
 	return ENOMEM;
