[libxslt] Fix double to int conversion
- From: Nick Wellnhofer <nwellnhof src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libxslt] Fix double to int conversion
- Date: Thu, 18 May 2017 16:25:41 +0000 (UTC)
commit df87857190d008b2d14fb500dac05c878c9ad1d7
Author: Nick Wellnhofer <wellnhofer aevum de>
Date: Thu May 18 17:14:28 2017 +0200
Fix double to int conversion
Add range checks to avoid undefined behavior.
Limit str:padding length to 100,000 chars.
libexslt/date.c | 44 ++++++++++++++++++++++++--------------------
libexslt/strings.c | 12 +++++++++++-
2 files changed, 35 insertions(+), 21 deletions(-)
---
diff --git a/libexslt/date.c b/libexslt/date.c
index ba7097d..220d71d 100644
--- a/libexslt/date.c
+++ b/libexslt/date.c
@@ -2096,11 +2096,12 @@ exsltDateMonthName (const xmlChar *dateTime)
{ 'N', 'o', 'v', 'e', 'm', 'b', 'e', 'r', 0 },
{ 'D', 'e', 'c', 'e', 'm', 'b', 'e', 'r', 0 }
};
- int month;
- month = (int) exsltDateMonthInYear(dateTime);
- if (!VALID_MONTH(month))
- month = 0;
- return monthNames[month];
+ double month;
+ int index = 0;
+ month = exsltDateMonthInYear(dateTime);
+ if (!xmlXPathIsNaN(month) && (month >= 1.0) && (month <= 12.0))
+ index = (int) month;
+ return monthNames[index];
}
/**
@@ -2144,11 +2145,12 @@ exsltDateMonthAbbreviation (const xmlChar *dateTime)
{ 'N', 'o', 'v', 0 },
{ 'D', 'e', 'c', 0 }
};
- int month;
- month = (int) exsltDateMonthInYear(dateTime);
- if(!VALID_MONTH(month))
- month = 0;
- return monthAbbreviations[month];
+ double month;
+ int index = 0;
+ month = exsltDateMonthInYear(dateTime);
+ if (!xmlXPathIsNaN(month) && (month >= 1.0) && (month <= 12.0))
+ index = (int) month;
+ return monthAbbreviations[index];
}
/**
@@ -2504,11 +2506,12 @@ exsltDateDayName (const xmlChar *dateTime)
{ 'F', 'r', 'i', 'd', 'a', 'y', 0 },
{ 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y', 0 }
};
- int day;
- day = (int) exsltDateDayInWeek(dateTime);
- if((day < 1) || (day > 7))
- day = 0;
- return dayNames[day];
+ double day;
+ int index = 0;
+ day = exsltDateDayInWeek(dateTime);
+ if(!xmlXPathIsNaN(day) && (day >= 1.0) && (day <= 7.0))
+ index = (int) day;
+ return dayNames[index];
}
/**
@@ -2544,11 +2547,12 @@ exsltDateDayAbbreviation (const xmlChar *dateTime)
{ 'F', 'r', 'i', 0 },
{ 'S', 'a', 't', 0 }
};
- int day;
- day = (int) exsltDateDayInWeek(dateTime);
- if((day < 1) || (day > 7))
- day = 0;
- return dayAbbreviations[day];
+ double day;
+ int index = 0;
+ day = exsltDateDayInWeek(dateTime);
+ if(!xmlXPathIsNaN(day) && (day >= 1.0) && (day <= 7.0))
+ index = (int) day;
+ return dayAbbreviations[index];
}
/**
diff --git a/libexslt/strings.c b/libexslt/strings.c
index 62f76fb..9be90b5 100644
--- a/libexslt/strings.c
+++ b/libexslt/strings.c
@@ -342,6 +342,7 @@ exsltStrDecodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) {
static void
exsltStrPaddingFunction (xmlXPathParserContextPtr ctxt, int nargs) {
int number, str_len = 0, str_size = 0;
+ double floatval;
xmlChar *str = NULL, *ret = NULL;
if ((nargs < 1) || (nargs > 2)) {
@@ -361,7 +362,16 @@ exsltStrPaddingFunction (xmlXPathParserContextPtr ctxt, int nargs) {
str_size = 1;
}
- number = (int) xmlXPathPopNumber(ctxt);
+ floatval = xmlXPathPopNumber(ctxt);
+
+ if (xmlXPathIsNaN(floatval) || floatval < 0.0) {
+ number = 0;
+ } else if (floatval >= 100000.0) {
+ number = 100000;
+ }
+ else {
+ number = (int) floatval;
+ }
if (number <= 0) {
xmlXPathReturnEmptyString(ctxt);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]