Index: kernel/generic/src/lib/string.c
===================================================================
--- kernel/generic/src/lib/string.c	(revision 32704cb4dbae5f6cf8c7e510153454648b83135e)
+++ kernel/generic/src/lib/string.c	(revision b54d2f177d73478c2fc4d1d122eddfecc7393b6d)
@@ -60,6 +60,7 @@
  *
  * Decode a single UTF-8 character from a plain char NULL-terminated
- * string. Decoding starts at @index and this index is incremented
- * if the current UTF-8 string is encoded in more than a single byte.
+ * string. Decoding starts at @index and this index is moved to the
+ * beginning of the next character. In case of decoding error,
+ * index advances. However, index is never moved beyond (str+limit).
  *
  * @param str   Plain character NULL-terminated string.
@@ -79,8 +80,8 @@
 	int cbytes;		/* Number of continuation bytes. */
 
-	if (*index > limit)
+	if (*index + 1 > limit)
 		return invalch;
 
-	b0 = (uint8_t) str[*index];
+	b0 = (uint8_t) str[(*index)++];
 
 	/* Determine code length. */
@@ -115,6 +116,5 @@
 	/* Decode continuation bytes. */
 	while (cbytes > 0) {
-		b = (uint8_t) str[*index + 1];
-		++(*index);
+		b = (uint8_t) str[(*index)++];
 
 		/* Must be 10xxxxxx. */
@@ -135,6 +135,6 @@
  * Encode a single UTF-32 character as UTF-8 and store it into
  * the given buffer at @index. Encoding starts at @index and
- * this index is incremented if the UTF-8 character takes
- * more than a single byte.
+ * this index is moved at the position where the next character
+ * can be written to.
  *
  * @param ch    Input UTF-32 character.
@@ -157,5 +157,5 @@
 	int i;
 
-	if (*index > limit)
+	if (*index >= limit)
 		return false;
 
@@ -185,5 +185,5 @@
 
 	/* Check for available space in buffer. */
-	if (*index + cbytes > limit)
+	if (*index + cbytes >= limit)
 		return false;
 
@@ -198,5 +198,5 @@
 
 	/* Advance index. */
-	*index += cbytes;
+	*index += (1 + cbytes);
 	
 	return true;
@@ -220,11 +220,18 @@
 	size_t size = 0;
 	index_t index = 0;
-	
-	while ((utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) && (size < count)) {
+	index_t iprev;
+	wchar_t ch;
+	
+	while (true) {
+		iprev = index;
+		if (size >= count)
+			break;
+		ch = utf8_decode(str, &index, UTF8_NO_LIMIT);
+		if (ch == '\0') break;
+
 		size++;
-		index++;
-	}
-	
-	return index;
+	}
+	
+	return iprev;
 }
 
@@ -284,5 +291,4 @@
 	while (utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) {
 		size++;
-		index++;
 	}
 	
