@@ -8141,30 +8141,34 @@ xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
8141
8141
*/
8142
8142
void
8143
8143
xmlXPathSubstringBeforeFunction (xmlXPathParserContextPtr ctxt , int nargs ) {
8144
- xmlXPathObjectPtr str ;
8145
- xmlXPathObjectPtr find ;
8146
- xmlBufPtr target ;
8147
- const xmlChar * point ;
8148
- int offset ;
8149
-
8150
- CHECK_ARITY (2 );
8151
- CAST_TO_STRING ;
8152
- find = valuePop (ctxt );
8153
- CAST_TO_STRING ;
8154
- str = valuePop (ctxt );
8155
-
8156
- target = xmlBufCreate ();
8157
- if (target ) {
8144
+ xmlXPathObjectPtr str = NULL ;
8145
+ xmlXPathObjectPtr find = NULL ;
8146
+ const xmlChar * point ;
8147
+ xmlChar * result ;
8148
+
8149
+ CHECK_ARITY (2 );
8150
+ CAST_TO_STRING ;
8151
+ find = valuePop (ctxt );
8152
+ CAST_TO_STRING ;
8153
+ str = valuePop (ctxt );
8154
+ if (ctxt -> error != 0 )
8155
+ goto error ;
8156
+
8158
8157
point = xmlStrstr (str -> stringval , find -> stringval );
8159
- if (point ) {
8160
- offset = point - str -> stringval ;
8161
- xmlBufAdd (target , str -> stringval , offset );
8158
+ if (point == NULL ) {
8159
+ result = xmlStrdup (BAD_CAST "" );
8160
+ } else {
8161
+ result = xmlStrndup (str -> stringval , point - str -> stringval );
8162
8162
}
8163
- valuePush (ctxt , xmlXPathCacheNewString (ctxt , xmlBufContent (target )));
8164
- xmlBufFree (target );
8165
- }
8166
- xmlXPathReleaseObject (ctxt -> context , str );
8167
- xmlXPathReleaseObject (ctxt -> context , find );
8163
+ if (result == NULL ) {
8164
+ xmlXPathPErrMemory (ctxt );
8165
+ goto error ;
8166
+ }
8167
+ valuePush (ctxt , xmlXPathCacheWrapString (ctxt , result ));
8168
+
8169
+ error :
8170
+ xmlXPathReleaseObject (ctxt -> context , str );
8171
+ xmlXPathReleaseObject (ctxt -> context , find );
8168
8172
}
8169
8173
8170
8174
/**
@@ -8176,38 +8180,41 @@ xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
8176
8180
* string substring-after(string, string)
8177
8181
* The substring-after function returns the substring of the first
8178
8182
* argument string that follows the first occurrence of the second
8179
- * argument string in the first argument string, or the empty stringi
8183
+ * argument string in the first argument string, or the empty string
8180
8184
* if the first argument string does not contain the second argument
8181
8185
* string. For example, substring-after("1999/04/01","/") returns 04/01,
8182
8186
* and substring-after("1999/04/01","19") returns 99/04/01.
8183
8187
*/
8184
8188
void
8185
8189
xmlXPathSubstringAfterFunction (xmlXPathParserContextPtr ctxt , int nargs ) {
8186
- xmlXPathObjectPtr str ;
8187
- xmlXPathObjectPtr find ;
8188
- xmlBufPtr target ;
8189
- const xmlChar * point ;
8190
- int offset ;
8191
-
8192
- CHECK_ARITY (2 );
8193
- CAST_TO_STRING ;
8194
- find = valuePop (ctxt );
8195
- CAST_TO_STRING ;
8196
- str = valuePop (ctxt );
8197
-
8198
- target = xmlBufCreate ();
8199
- if (target ) {
8190
+ xmlXPathObjectPtr str = NULL ;
8191
+ xmlXPathObjectPtr find = NULL ;
8192
+ const xmlChar * point ;
8193
+ xmlChar * result ;
8194
+
8195
+ CHECK_ARITY (2 );
8196
+ CAST_TO_STRING ;
8197
+ find = valuePop (ctxt );
8198
+ CAST_TO_STRING ;
8199
+ str = valuePop (ctxt );
8200
+ if (ctxt -> error != 0 )
8201
+ goto error ;
8202
+
8200
8203
point = xmlStrstr (str -> stringval , find -> stringval );
8201
- if (point ) {
8202
- offset = point - str -> stringval + xmlStrlen ( find -> stringval );
8203
- xmlBufAdd ( target , & str -> stringval [ offset ],
8204
- xmlStrlen (str -> stringval ) - offset );
8204
+ if (point == NULL ) {
8205
+ result = xmlStrdup ( BAD_CAST "" );
8206
+ } else {
8207
+ result = xmlStrdup ( point + xmlStrlen (find -> stringval ));
8205
8208
}
8206
- valuePush (ctxt , xmlXPathCacheNewString (ctxt , xmlBufContent (target )));
8207
- xmlBufFree (target );
8208
- }
8209
- xmlXPathReleaseObject (ctxt -> context , str );
8210
- xmlXPathReleaseObject (ctxt -> context , find );
8209
+ if (result == NULL ) {
8210
+ xmlXPathPErrMemory (ctxt );
8211
+ goto error ;
8212
+ }
8213
+ valuePush (ctxt , xmlXPathCacheWrapString (ctxt , result ));
8214
+
8215
+ error :
8216
+ xmlXPathReleaseObject (ctxt -> context , str );
8217
+ xmlXPathReleaseObject (ctxt -> context , find );
8211
8218
}
8212
8219
8213
8220
/**
0 commit comments