pecl-jsmin icon indicating copy to clipboard operation
pecl-jsmin copied to clipboard

Illegal chars at the end of the string

Open osopolar opened this issue 9 years ago • 11 comments

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 avatar Jan 15 '16 00:01 osopolar

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?

osopolar avatar Jan 28 '16 16:01 osopolar

I also saw this.

netguy2k15 avatar Jun 20 '16 05:06 netguy2k15

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

osopolar avatar Jun 20 '16 05:06 osopolar

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 avatar Jun 20 '16 06:06 netguy2k15

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.

netguy2k15 avatar Jun 20 '16 10:06 netguy2k15

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

pine3ree avatar Dec 26 '16 14:12 pine3ree

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 avatar Jan 20 '17 04:01 das-peter

Issue #44 still occurs in the latest code base

das-peter avatar Jan 20 '17 20:01 das-peter

Also affected.

RafalLukawiecki avatar Dec 23 '17 10:12 RafalLukawiecki

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.

cherrador avatar Jun 27 '18 23:06 cherrador

Did anyone every solve this in the actual c code?

tpneumat avatar May 19 '21 14:05 tpneumat