--- linux-2.6.1-joe_korty-bitmap/lib/bitmap.c.orig Mon Jan 19 11:45:32 2004 +++ linux-2.6.1-joe_korty-bitmap/lib/bitmap.c Mon Jan 19 13:11:24 2004 @@ -209,13 +209,13 @@ EXPORT_SYMBOL(bitmap_snprintf); * bits of the resultant bitmask. No chunk may specify a value larger * than 32 bits (-EOVERFLOW), and if a chunk specifies a smaller value * then leading 0-bits are prepended. -EINVAL is returned for illegal - * characters and for grouping errors such as "1,,5", ,44", "," and "". + * characters and for grouping errors such as "1,,5", ",44", "," and "". * Leading and trailing whitespace accepted, but not embedded whitespace. */ int bitmap_parse(const char __user *ubuf, unsigned int ubuflen, unsigned long *maskp, int nmaskbits) { - int i, c, oc, ndigits, totaldigits, nchunks, nbits; + int i, c, old_c, totaldigits, ndigits, nchunks, nbits; u32 chunk; bitmap_clear(maskp, nmaskbits); @@ -223,40 +223,73 @@ int bitmap_parse(const char __user *ubuf nchunks = nbits = totaldigits = c = 0; do { chunk = ndigits = 0; + + /* Get the next chunk of the bitmap */ while (ubuflen) { - oc = c; + /* Remember the last char & get the next char */ + old_c = c; if (get_user(c, ubuf++)) return -EFAULT; ubuflen--; + + /* Ignore spaces */ if (isspace(c)) continue; - if (totaldigits && c && isspace(oc)) + + /* + * If the last character was a space and the current + * character isn't '\0' we've got embedded whitespace. + * This is a no-no, so throw an error. + */ + if (totaldigits && c && isspace(old_c)) return -EINVAL; + + /* A '\0' or a ',' signal the end of the chunk */ if (!c || c == ',') break; + + /* A non-hexdigit is also a no-no, so throw an error */ if (!isxdigit(c)) return -EINVAL; + + /* + * Make sure there are at least 4 free bits in 'chunk'. + * If not, this hexdigit will overflow 'chunk', so + * throw an error. + */ if (chunk & ~((1UL << (CHUNKSZ - 4)) - 1)) return -EOVERFLOW; + + /* + * Add this expanded hexdigit to 'chunk' and increment + * both the current chunk & total digit counts. + */ chunk = (chunk << 4) | unhex(c); ndigits++; totaldigits++; } + /* Empty chunks are another no-no. Throw an error. */ if (ndigits == 0) return -EINVAL; + + /* If the first chunk is all 0's, just move to the next one */ if (nchunks == 0 && chunk == 0) continue; + + /* Shift the bitmap right to make room for this chunk */ bitmap_shift_right(maskp, maskp, CHUNKSZ, nmaskbits); + + /* Copy the chunk into the bitmap, and increment chunk count */ for (i = 0; i < CHUNKSZ; i++) if (chunk & (1 << i)) set_bit(i, maskp); nchunks++; + + /* Increment the bit count & make sure we didn't overflow */ nbits += (nchunks == 1) ? nbits_to_hold_value(chunk) : CHUNKSZ; if (nbits > nmaskbits) return -EOVERFLOW; } while (ubuflen && c == ','); - if (totaldigits == 0) - return -EINVAL; return 0; } EXPORT_SYMBOL(bitmap_parse);