vohoaiviet / kaptcha

Automatically exported from code.google.com/p/kaptcha
Other
0 stars 0 forks source link

DefaultTextProducer ignores first character in a configured char string #37

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Get kaptcha up and running :-)
2. Configure a simple textproducer char string for the DefaultTextCreator,
i.e. use the following parameter:
kaptcha.textproducer.char.string="abc"

What is the expected output? What do you see instead?
Captcha image containing a random sequence of the configured characters.
Instead, only the characters "b" and "c" appear.

What version of the product are you using? On what operating system?
kaptcha-2.3-jdk14.jar on Windows XP (will happen on any environment as the
error is in the source code, see below)

Please provide any additional information below.
Take a look at the getText method:

        int length = getConfig().getTextProducerCharLength();
        char[] chars = getConfig().getTextProducerCharString();
        int randomContext = chars.length - 1;
        Random rand = new Random();
        StringBuffer text = new StringBuffer();
        for (int i = 0; i < length; i++)
        {
            text.append(chars[rand.nextInt(randomContext) + 1]);
        }

        return text.toString();

Taken from the API doc of nextInt(n): "Returns a pseudorandom, uniformly
distributed int value between 0 (inclusive) and the specified value
(exclusive)."

So nextInt(randomContext) generates a number from 0 to length-2 (both
including), hence nextInt(randomContext)+1 is a number from 1 to length-1.
Therefore the first char is always missed. The code should instead simply
look like this:

        int length = getConfig().getTextProducerCharLength();
        char[] chars = getConfig().getTextProducerCharString();
        Random rand = new Random();
        StringBuffer text = new StringBuffer();
        for (int i = 0; i < length; i++)
        {
            text.append(chars[rand.nextInt(length)]);
        }

        return text.toString();

I have added this as a patch file if that is needed for a simple change
like this :-)

Original issue reported on code.google.com by jchoffm...@gmail.com on 9 Feb 2009 at 5:36

Attachments:

GoogleCodeExporter commented 9 years ago
wow. finally a real bug report and a patch (vs. the crap other people submit). 
you rock. =)

i'll get that fixed in svn as soon as i have a chance.

Original comment by latch...@gmail.com on 9 Feb 2009 at 9:52

GoogleCodeExporter commented 9 years ago
Hey, thanks for answering so fast. I think it is a nice feature, keep up the 
good
work :-)

Original comment by jchoffm...@gmail.com on 10 Feb 2009 at 9:15

GoogleCodeExporter commented 9 years ago

Original comment by latch...@gmail.com on 17 Feb 2009 at 12:57

GoogleCodeExporter commented 9 years ago

Original comment by latch...@gmail.com on 16 Nov 2010 at 8:33

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r117.

Original comment by whiche...@gmail.com on 20 Nov 2010 at 7:28

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Get kaptcha up and running
2. Configure a simple textproducer char string for the DefaultTextCreator,
i.e. use the following parameters:
kaptcha.textproducer.char.string="abcefghijklmnopqrstuvwxyz"
kaptcha.textproducer.char.length=5

What is the expected output? What do you see instead?
Captcha image containing a random sequence of the configured characters.
Instead, only first 5 characters appear. This is due to previous bug fix on 
version 2.3

What version of the product are you using? On what operating system?
kaptcha-2.3.1.jar on Windows XP (will happen on any environment as the
error is in the source code, see below)

Please provide any additional information below.
Take a look at the getText method:

        int length = getConfig().getTextProducerCharLength();
    char[] chars = getConfig().getTextProducerCharString();
    Random rand = new Random();
    StringBuffer text = new StringBuffer();
    for (int i = 0; i < length; i++)
    {
        text.append(chars[rand.nextInt(length)]);
    }

    return text.toString();

This will only produce the first "length" (5) characters.

The correct version is to change line 25 adding "chars.length" instead of 
"length":

        int length = getConfig().getTextProducerCharLength();
    char[] chars = getConfig().getTextProducerCharString();
    Random rand = new Random();
    StringBuffer text = new StringBuffer();
    for (int i = 0; i < length; i++)
    {
        text.append(chars[rand.nextInt(chars.length)]);
    }

    return text.toString();

Attached is a patch to fix this. It simply changes line 25.

Original comment by fkim...@gmail.com on 25 Nov 2010 at 1:05

Attachments:

GoogleCodeExporter commented 9 years ago
ok, i've committed this fix.

Original comment by latch...@gmail.com on 5 Dec 2010 at 11:37