sqmk / pecl-jsmin

PHP PECL extension for JavaScript minification
Other
69 stars 21 forks source link

Illegal chars at the end of the string #46

Open osopolar opened 8 years ago

osopolar commented 8 years ago

Using JSMin in drupal module advagg, I get some console errors in Firefox and Chrome like "SyntaxError: illegal character", "Uncaught SyntaxError: Unexpected token ILLEGAL" or "SyntaxError: expected expression, got ')'". It seems there are some UTF-8 characters that don't belong there.

Other developers run into the same problem:

Under some unknown/rare circumstances, JSMin (and JSqueeze) can add 2-3 extraneous/wrong chars at the end of the string. The first chars varies a bit but the last char is always an ASCII control character.

Workaround ($contents contains the minified js code):

if (ctype_cntrl(substr(trim($contents),-1))) {
  $contents = substr($contents,0,strrpos($contents,';'));
}

See also issue https://www.drupal.org/node/2627468.

osopolar commented 8 years ago

This seems to happen only at the end of the string and may be related to multi-byte characters. Could it be possible that the script reads stuff from memory where it shouldn't read or gets something that didn't get cleared probably?

netguy2k15 commented 8 years ago

I also saw this.

osopolar commented 8 years ago

@netguy2k15 Do you have some more information which may help to reproduce this issue?

netguy2k15 commented 8 years ago

Now I just know that this function is invoked so many times, and Javascript also contains some multi-byte characters, let me try to reproduce it. Later I will also do some debugging.

netguy2k15 commented 8 years ago

Looks like if multi-byte characters is in a regular expression, it causes problems. Here is a related report: https://github.com/sqmk/pecl-jsmin/issues/44

If it's a regular expression, I suggest we just keep it and do not minify it.


Some updates: I fixed the regular issue and rebuilt, this can work, but it doesn't help the current problem.

pine3ree commented 7 years ago

Hello, I also have this issue. I sometimes get an unprintable char + a number after a } or a ; kind regards

das-peter commented 7 years ago

There was this pull request #49 which was committed https://github.com/sqmk/pecl-jsmin/pull/49/commits/0a291e0eb520de03d96c1acf2300588541deca61 Maybe this affected this issue as well. Will try to give it a test and report back.

das-peter commented 7 years ago

Issue #44 still occurs in the latest code base

RafalLukawiecki commented 6 years ago

Also affected.

cherrador commented 6 years ago

I get the same problem. It is quite random. The source could be anything imported UTF-8 file, WIN-1252 or inline code written inside the PHP file. Depending how the script is written, the whole minified script could be invalid (e.g. anonymous or delta function).

The same page could be refreshed and the problem is now gone. Refreshing it again and the page could show another different set of characters. Sometimes, somehow, they are valid Javascript and allows the code to run unnoticed to the end user with the suffixed extra characters.

Thanks to @osopolar for the startup workaround, however sometimes the characters could be printable characters. For example, sometimes I get the ^n at the end. I have improved the workaround like this:

$js=trim(jsmin(trim($js)));
//workaround for jsmin bug of random characters at the end
$l=substr($js,-1);
while(!empty($l) &&  $l!==';' && $l!=='}'){
    $js=substr($js,0,-1);
    $l=substr($js,-1);
}

This assumes that a valid good written script, the last character should be ; or}. Any other characters (control or printable) will be removed.

tpneumat commented 3 years ago

Did anyone every solve this in the actual c code?