Fix parsing of octal string escapes

We now stop when the next character is digit 8 or 9, and stop after 3
octal digits even if the next character is an octal digit.
This commit is contained in:
Olly Betts 2024-08-15 14:38:52 +12:00
parent 16680f59da
commit 4299e893da
4 changed files with 23 additions and 10 deletions

View File

@ -7,6 +7,11 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.3.0 (in progress)
===========================
2024-08-15: olly
Fix parsing of octal string escapes. We now stop when the next
character is digit 8 or 9, and stop after 3 octal digits even if
the next character is an octal digit.
2024-08-15: olly
SWIG now gives an error for digits 8 and 9 in octal constants -
previously these were quietly accepted resulting in a bogus value.

View File

@ -9,10 +9,10 @@ check::classes(array('string_constants', 'things'));
// New vars
check::globals(array('AA3', 'EE3', 'ES3', 'QQ3', 'SS3', 'XX3', 'ZS3'));
check::equal(string_constants::QQ1, "\01000!");
check::equal(string_constants::QQ2, "\01000!");
check::equal(QQ3_get(), "\01000!");
check::equal(string_constants::QQ1, "\01000! \0018b00!");
check::equal(string_constants::QQ2, "\01000! \0018b00!");
check::equal(QQ3_get(), "\01000! \0018b00!");
$t = new things();
check::equal($t->defarguments7(), "\01000!");
check::equal($t->defarguments7(), "\01000! \0018b00!");
check::done();

View File

@ -18,7 +18,7 @@
#define XX1 "\x57\x58\x59"
#define ZS1 "\0"
#define ES1 ""
#define QQ1 "\b00!"
#define QQ1 "\b00! \18\14200!"
%}
%constant SS2="ÆÎOU\n";
%constant AA2="A\rB\nC";
@ -26,7 +26,7 @@
%constant XX2="\x57\x58\x59";
%constant ZS2="\0";
%constant ES2="";
%constant QQ2="\b00!";
%constant QQ2="\b00! \18\14200!";
%inline %{
static const char *SS3 = "ÆÎOU\n";
@ -35,7 +35,7 @@ static const char *EE3 = "\124\125\126";
static const char *XX3 = "\x57\x58\x59";
static const char *ZS3 = "\0";
static const char *ES3 = "";
static const char *QQ3 = "\b00!";
static const char *QQ3 = "\b00! \18\14200!";
struct things {
const char * defarguments1(const char *SS4 = "ÆÎOU\n") { return SS4; }
const char * defarguments2(const char *AA4 = "A\rB\nC") { return AA4; }
@ -43,6 +43,6 @@ struct things {
const char * defarguments4(const char *XX4 = "\x57\x58\x59") { return XX4; }
const char * defarguments5(const char *ZS4 = "\0") { return ZS4; }
const char * defarguments6(const char *ES4 = "") { return ES4; }
const char * defarguments7(const char *QQ4 = "\b00!") { return QQ4; }
const char * defarguments7(const char *QQ4 = "\b00! \18\14200!") { return QQ4; }
};
%}

View File

@ -474,14 +474,22 @@ static void get_escape(Scanner *s) {
return;
}
break;
case 10:
if (!isdigit(c)) {
case 10: // Second digit of octal escape sequence
case 11: // Third digit of octal escape sequence
if (c < '0' || c > '7') {
retract(s,1);
Putc((char)result,s->text);
return;
}
result = (result << 3) + (c - '0');
Delitem(s->text, DOH_END);
if (state == 11) {
if (result > 255)
Swig_error(Scanner_file(s), Scanner_line(s), "octal escape sequence out of range\n");
Putc((char)result,s->text);
return;
}
state = 11;
break;
case 20:
if (!isxdigit(c)) {