Skip to content

Commit cbfb538

Browse files
committed
fixed #5 enhanced Warning messages
1 parent 4a5cc39 commit cbfb538

6 files changed

+200
-7
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,30 @@ if ( ($buf = filemagic ('modules/magic.so', MAGIC_MIME, '/usr/share/misc/magic.m
140140
Resut:
141141
application/x-sharedlib; charset=binary
142142
```
143+
144+
**filemagic function error control :**
145+
```php
146+
<?php
147+
if ( ($buf = filemagic ('wrong_path')) == false ) {
148+
if ( version_comapre(PHP_VERSION, '7.2.0', '<') ) {
149+
ini_set ('track_errors', true)
150+
printf ("ERROR: %s\n", $php_errormsg);
151+
}
152+
$err = error_get_last ();
153+
print_r ($err);
154+
}
155+
?>
156+
157+
Result:
158+
ERROR: wrong_path file not found.
159+
Array
160+
(
161+
[type] => 2
162+
[message] => wrong_path file not found.
163+
[file] => /some/path/test.php
164+
[line] => 2
165+
)
166+
```
167+
- $php_errormsg must have the track_errors setting enabled.
168+
- $php_errormsg was deprecated in 7.2.0.
169+
- The filemagic function does not call any error handlers when an error occurs. However, the error message is assigned to $php_errormsg or error_get_last ().

magic.c

+102-7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,101 @@ PHP_MINIT_FUNCTION(magic)
148148
}
149149
/* }}} */
150150

151+
/* {{{ static void magic_set_error (int type, const char * format, ...)
152+
*/
153+
static void magic_set_error (int type, const char * format, ...) {
154+
va_list args;
155+
char * buffer = NULL;
156+
int buffer_len = 0;
157+
#if PHP_VERSION_ID >= 80000
158+
zend_string * zbuffer;
159+
#endif
160+
161+
// php_error_cb in main.c
162+
va_start (args, format);
163+
buffer_len = vspprintf (&buffer, PG(log_errors_max_len), format, args);
164+
va_end (args);
165+
166+
#if PHP_VERSION_ID >= 80000
167+
zbuffer = zend_string_init (buffer, buffer_len, 0);
168+
#endif
169+
170+
#if PHP_VERSION_ID >= 80000
171+
if (PG(last_error_message)) {
172+
zend_string_release(PG(last_error_message));
173+
PG(last_error_message) = NULL;
174+
}
175+
if (PG(last_error_file)) {
176+
free(PG(last_error_file));
177+
PG(last_error_file) = NULL;
178+
}
179+
#else
180+
if ( PG (last_error_message) ) {
181+
#if PHP_VERSION_ID >= 70200
182+
char * s = PG(last_error_message);
183+
PG(last_error_message) = NULL;
184+
free (s);
185+
#else
186+
free (PG (last_error_message));
187+
PG (last_error_message) = NULL;
188+
#endif
189+
}
190+
if ( PG(last_error_file) ) {
191+
#if PHP_VERSION_ID >= 70200
192+
char * s = PG(last_error_file);
193+
PG(last_error_file) = NULL;
194+
free (s);
195+
#else
196+
free (PG (last_error_file));
197+
PG (last_error_file) = NULL;
198+
#endif
199+
}
200+
#endif
201+
PG (last_error_lineno) = 0;
202+
203+
/*
204+
* last_error_file, last_error_lineno
205+
* see also zend_error in zend.c
206+
* -> zend_get_compiled_filename
207+
* -> zend_get_executed_lineno
208+
*/
209+
PG (last_error_type) = type;
210+
#if PHP_VERSION_ID >= 80000
211+
PG (last_error_message) = zend_string_copy (zbuffer);
212+
zend_string_release_ex (zbuffer, 0);
213+
#else
214+
PG (last_error_message) = strdup (buffer);
215+
#endif
216+
if ( zend_is_compiling () ) {
217+
PG (last_error_file) = strdup (ZSTR_VAL (zend_get_compiled_filename ()));
218+
PG (last_error_lineno) = zend_get_compiled_lineno ();
219+
} else if ( zend_is_executing () ) {
220+
PG (last_error_file) = strdup (zend_get_executed_filename ());
221+
PG (last_error_lineno) = zend_get_executed_lineno ();
222+
}
223+
224+
#if PHP_VERSION_ID < 70200
225+
/*
226+
* $php_errormsg don't support 7.2.0 and after.
227+
*/
228+
if ( PG(track_errors) ) {
229+
zval tmp;
230+
231+
ZVAL_STRINGL(&tmp, buffer, buffer_len);
232+
if (EG(current_execute_data)) {
233+
if (zend_set_local_var_str("php_errormsg", sizeof("php_errormsg")-1, &tmp, 0) == FAILURE) {
234+
zval_ptr_dtor(&tmp);
235+
}
236+
} else {
237+
zend_hash_str_update_ind(&EG(symbol_table), "php_errormsg", sizeof("php_errormsg")-1, &tmp);
238+
}
239+
}
240+
#endif
241+
if ( buffer_len > 0 )
242+
efree (buffer);
243+
}
244+
/* }}} */
245+
151246
/* {{{ proto filemagic(string path[, int flag[, string magic-path]]): string|false
152247
* proto filemagic(string path[, string magic-path[, int flag]]): string|false
153248
*/
@@ -180,12 +275,12 @@ ZEND_FUNCTION(filemagic) {
180275
return;
181276

182277
if ( ZSTR_LEN (path) == 0 ) {
183-
php_error (E_WARNING, "The value of 1st argument was empty.");
278+
magic_set_error (E_WARNING, "The value of 1st argument was empty.");
184279
RETURN_FALSE;
185280
}
186281

187282
if ( stat (ZSTR_VAL (path), &filestat) != 0 ) {
188-
php_error (E_WARNING, "%s file not found.", ZSTR_VAL (path));
283+
magic_set_error (E_WARNING, "%s file not found.", ZSTR_VAL (path));
189284
RETURN_FALSE;
190285
}
191286

@@ -202,7 +297,7 @@ ZEND_FUNCTION(filemagic) {
202297
mpath = MAGIC;
203298
break;
204299
default :
205-
php_error (E_WARNING, "2th argument is only available for integer(flag) or MAGIC file path.");
300+
magic_set_error (E_WARNING, "2th argument is only available for integer(flag) or MAGIC file path.");
206301
RETURN_FALSE;
207302
}
208303
} else if ( fargs == 3 ) {
@@ -213,7 +308,7 @@ ZEND_FUNCTION(filemagic) {
213308
flag = Z_LVAL_P (zpath);
214309
mpath = Z_STRLEN_P (zflag) ? Z_STRVAL_P (zflag) : MAGIC;
215310
} else {
216-
php_error (E_WARNING, "The 2th and 3th argument can only be integer or strings.");
311+
magic_set_error (E_WARNING, "The 2th and 3th argument can only be integer or strings.");
217312
RETURN_FALSE;
218313
}
219314
}
@@ -223,18 +318,18 @@ ZEND_FUNCTION(filemagic) {
223318

224319
mp = magic_open (flag);
225320
if ( mp == NULL ) {
226-
php_error (E_WARNING, strerror (errno));
321+
magic_set_error (E_WARNING, strerror (errno));
227322
RETURN_FALSE;
228323
}
229324

230325
if ( magic_load (mp, mpath) == -1 ) {
231-
php_error (E_WARNING, magic_error (mp));
326+
magic_set_error (E_WARNING, magic_error (mp));
232327
magic_close (mp);
233328
RETURN_FALSE;
234329
}
235330

236331
if ( (type = magic_file (mp, ZSTR_VAL (path))) == NULL ) {
237-
php_error (E_WARNING, magic_error(mp));
332+
magic_set_error (E_WARNING, magic_error(mp));
238333
magic_close (mp);
239334
RETURN_FALSE;
240335
}

package.xml.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- #1 remove useless code
2727
- #3 confim to support until PHP 8.0
2828
- #4 enhanced argument order of filebin function
29+
- #5 enhanced Warning messges
2930
- #6 change package name to mod_magic from mod_filebin
3031
- #7 improved usage documents
3132
</notes>
@@ -67,6 +68,7 @@
6768
- #1 remove useless code
6869
- #3 confim to support until PHP 8.0
6970
- #4 enhanced argument order of filebin function
71+
- #5 enhanced Warning messges
7072
- #6 change package name to mod_magic from mod_filebin
7173
- #7 improved usage documents
7274
</notes>

sample.php

+19
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,23 @@
6060

6161
echo filemagic ('modules/magic.so', '/usr/share/misc/magic.mgc', MAGIC_MIME) . "\n";
6262

63+
echo <<<EOF
64+
65+
*
66+
* error controls:
67+
*
68+
* if ( filemagic ('wrong_path', MAGIC_MIME) == false ) {
69+
* ini_set ('track_errors', true);
70+
* echo "## php_errormsg : " . \$php_errormsg . "\\n";
71+
* print_r (error_get_last ());
72+
* }
73+
*
74+
75+
EOF;
76+
77+
if ( filemagic ('wrong_path', MAGIC_MIME) == false ) {
78+
ini_set ('track_errors', true);
79+
echo "## php_errormsg : " . $php_errormsg . "\n";
80+
print_r (error_get_last ());
81+
}
6382
?>

tests/007-php-errormsg.phpt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Check for $php_errormsg variables
3+
--SKIPIF--
4+
<?php
5+
if ( ! extension_loaded ('magic') ) {
6+
print 'skip';
7+
}
8+
?>
9+
--POST--
10+
--GET--
11+
--INI--
12+
--FILE--
13+
<?php
14+
15+
if ( version_compare (PHP_VERSION, '7.2.0', '>=') ) {
16+
# from php 7.2.0, don't support $php_errormsg
17+
echo "modules/magic.so1 file not found.";
18+
} else {
19+
if ( filemagic ('modules/magic.so1') == false ) {
20+
ini_set ('track_errors', true);
21+
echo $php_errormsg . "\n";
22+
}
23+
}
24+
?>
25+
--EXPECT--
26+
modules/magic.so1 file not found.

tests/008-error-get-last.phpt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Check for error_get_last function
3+
--SKIPIF--
4+
<?php
5+
if ( ! extension_loaded ('magic') ) {
6+
print 'skip';
7+
}
8+
?>
9+
--POST--
10+
--GET--
11+
--INI--
12+
--FILE--
13+
<?php
14+
if ( filemagic ('modules/magic.so1') == false ) {
15+
$p = error_get_last ();
16+
if ( $p['type'] == E_WARNING )
17+
$s = 'E_WARNING';
18+
else
19+
$s = 'E_UNKNONW';
20+
printf ("%s: %s\n", $s, $p['message']);
21+
}
22+
?>
23+
--EXPECT--
24+
E_WARNING: modules/magic.so1 file not found.

0 commit comments

Comments
 (0)