Skip to content

Commit

Permalink
better solution for tclDate.c (since Number is not an int) avoiding o…
Browse files Browse the repository at this point in the history
…verflow-trap; cherry-pick from tcl-core
  • Loading branch information
sebres committed Mar 15, 2024
1 parent a1788d5 commit 8067e86
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
17 changes: 4 additions & 13 deletions generic/tclDate.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
typedef struct _TABLE {
const char *name;
int type;
Tcl_WideInt value;
long long value;
} TABLE;

/*
Expand Down Expand Up @@ -224,7 +224,7 @@ extern int TclDatedebug;
union YYSTYPE
{

Tcl_WideInt Number;
long long Number;
enum _MERIDIAN Meridian;


Expand Down Expand Up @@ -2676,11 +2676,6 @@ LookupWord(
return tID;
}

/* int overflows may happens here (expected case) */
#if defined(__GNUC__) || defined(__GNUG__)
# pragma GCC optimize("no-trapv")
#endif

static int
TclDatelex(
YYSTYPE* yylvalPtr,
Expand Down Expand Up @@ -2711,7 +2706,7 @@ TclDatelex(
/*
* Convert the string into a number; count the number of digits.
*/
int num = c - '0';
long long num = c - '0';
p = (char *)yyInput;
while (isdigit(UCHAR(c = *(++p)))) {
if (num >= 0) {
Expand Down Expand Up @@ -2743,7 +2738,7 @@ TclDatelex(
location->last_column = yyInput - info->dateStart - 1;
return tISOBASL;
}
if (num < 0) { /* overflow */
if (yyDigitCount > 14) { /* overflow */
return tID;
}
if (yyDigitCount == 8) {
Expand Down Expand Up @@ -2818,10 +2813,6 @@ TclDatelex(
} while (Count > 0);
}
}

#if defined(__GNUC__) || defined(__GNUG__)
# pragma GCC reset_options
#endif

int
TclClockFreeScan(
Expand Down
17 changes: 4 additions & 13 deletions generic/tclGetDate.y
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
typedef struct _TABLE {
const char *name;
int type;
Tcl_WideInt value;
long long value;
} TABLE;

/*
Expand All @@ -103,7 +103,7 @@ typedef enum _DSTMODE {
%}

%union {
Tcl_WideInt Number;
long long Number;
enum _MERIDIAN Meridian;
}

Expand Down Expand Up @@ -877,11 +877,6 @@ LookupWord(
return tID;
}

/* int overflows may happens here (expected case) */
#if defined(__GNUC__) || defined(__GNUG__)
# pragma GCC optimize("no-trapv")
#endif

static int
TclDatelex(
YYSTYPE* yylvalPtr,
Expand Down Expand Up @@ -912,7 +907,7 @@ TclDatelex(
/*
* Convert the string into a number; count the number of digits.
*/
int num = c - '0';
long long num = c - '0';
p = (char *)yyInput;
while (isdigit(UCHAR(c = *(++p)))) {
if (num >= 0) {
Expand Down Expand Up @@ -944,7 +939,7 @@ TclDatelex(
location->last_column = yyInput - info->dateStart - 1;
return tISOBASL;
}
if (num < 0) { /* overflow */
if (yyDigitCount > 14) { /* overflow */
return tID;
}
if (yyDigitCount == 8) {
Expand Down Expand Up @@ -1019,10 +1014,6 @@ TclDatelex(
} while (Count > 0);
}
}

#if defined(__GNUC__) || defined(__GNUG__)
# pragma GCC reset_options
#endif

int
TclClockFreeScan(
Expand Down

0 comments on commit 8067e86

Please sign in to comment.