Actions
Bug #11307
closedSeveral validators don't copy WString before applying args
Start date:
02/03/2023
Due date:
% Done:
100%
Estimated time:
Description
Many validators allow changing the error message, e.g. WIntValidator
has a setInvalidTooSmallText(...)
.
If this setter is used, then calls to invalidTooSmallText()
will not create a copy of that string, but they will bind arguments to it:
WIntValidator intValidator = new WIntValidator();
intValidator.setBottom(1);
WString text = new WString("number must be between {1} and {2}");
intValidator.setInvalidTooSmallText(text);
WString tooSmall = intValidator.getInvalidTooSmallText();
assert text != tooSmall; // ASSERTION ERROR
Further calls to getInvalidTooSmallText()
will bind more parameters:
WString tooSmall2 = intValidator.getInvalidTooSmallText();
assert tooSmall2.getArgs().size() == 2; // ASSERTION ERROR, getArgs().size() is 4!
The reason for this is that while Wt makes a copy of the original WString, like:
WString s = tooSmallText_; // Copy constructed used here
s.arg(bottom_).arg(top_);
return s;
However, in JWt this is translated to:
WString s = this.tooSmallText_; // Reference assignment, s and tooSmallText_ point to the same object!
s.arg(this.bottom_).arg(this.top_);
return s;
I propose that we either add a copy constructor to WString
in JWt, or implement the clone()
function, and make sure we use it.
I have verified that the following validators are affected:
- WIntValidator
- WDoubleValidator
- WDateValidator
- WTimeValidator
- WLengthValidator
Actions