fogbound.net




Tue, 7 Jun 2016

JavaScript compares things weirdly

— SjG @ 2:52 pm

We’ve already established that PHP compares things weirdly.

It shouldn’t surprise us that JavaScript does too.

Consider the following:

> var k=['hello'];
undefined
> (k=='hello'?'Equals':'Nope');
Equals

Now, purists will point out that that’s an “equals” operator not an “identity” operator, but I mean seriously? We’re just going to pretend that


> ['hello']=='hello'
true

I think I’ll just go and rewrite all my client side code in C now.


Mon, 28 Mar 2016

PHP Compares Things Weirdly

— SjG @ 10:36 am

This is a known .. uh … situation, but it bit me today.

So, consider the following:
$ php --version
PHP 5.4.16 (cli) (built: Jun 23 2015 21:17:27)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
$ php -a
Interactive shell
php > $v1 = '479014103257633139480';
php > $v2 = '479014103257633139481';
php > echo ($v1==$v2?'Equal':'Not Equal');
Not Equal

Seems sane, yes? Reasonable. Kind of what you expect.

But then, consider this:

$ php --version
PHP 5.3.3 (cli) (built: Feb 9 2016 10:36:17)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
$ php -a
Interactive shell
php > $v1 = '479014103257633139480';
php > $v2 = '479014103257633139481';
php > echo ($v1==$v2?'Equal':'Not Equal');
Equal

Yeah. Let that sink in for a moment.

Some versions of PHP (before 5.4.mumble) will preƫmptively convert strings to numbers before comparing them (if they contain only digits). But if the number is large enough, you may lose the precision to compare them correctly.

Wow. I mean, just … well… I dunno.

For what it’s worth, strcmp will do the right thing regardless of PHP version. But seriously. I mean. Why do I use this turdburger of a language?


Sun, 18 Oct 2015

Yii mystery on Mac

— SjG @ 3:07 pm

I upgraded the Mac to Yosemite a year or so ago. Yesterday, I wanted to do some development on a project that I’d been idly thinking about. Unfortunately, it required a dependency in a package I’d installed via Mac Ports. I tried to upgrade it, but got an error that I was compiling for the wrong Darwin version. This means I haven’t actually updated any of my Ports since upgrading to Yosemite! For shame.

Rather than fix Mac Ports for Yosemite, and then again when I upgrade to El Capitan, I decided it was time to do that upgrade and then fix it. I went through and did so, and upgraded all my ports, and it all seemed to go well. I went from PHP 5.3 to PHP 5.5, and MySQL from 5.1 to 5.5, and it went without a snag — the web server came up and my old configuration was good, databases same. Everything seemed sweet and easy!

Ha! Sweet and easy with software? Not so fast, buckeroo! Working on another project, I found that Yii was crashing — but only from the command line!
exception 'CDbException' with message 'CDbConnection failed to open the DB connection: could not find driver' in /Users/samuelg/project/unicorn_rainbows/framework/db/CDbConnection.php:399

I quickly through a page up with phpinfo(), and saw that all the usual suspects were valid:

  • I was running the PHP I thought I was (Mac Ports version 5.5, not the built-in Mac OS version)
  • It was using the php.ini file I thought it was (in /opt/local/etc/php55/)
  • PDO was installed
  • PDO’s MySQL driver was installed
  • PHP’s configured recognized the drivers
  • Paths to any config files were correct
  • Ports were all normal
  • Default path to MySQL socket file was correct

Of course, this all makes sense, because my web pages that access the database were working.

So why not from Yii’s console program from the command line?

A quick php -version revealed the problem. It wasn’t the database configuration at all! Well, not exactly.
samuelg$ php --version
PHP 5.6.14 (cli) (built: Oct 15 2015 16:20:41)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

Wait, what? PHP 5.6? But… how?

samuel$ port installed | grep php
...
php55-mysql @5.5.30_0+mysqlnd (active)
php55-sqlite @5.5.30_0 (active)
php56 @5.6.6_0+libedit
php56 @5.6.14_0+libedit (active)

Yup, somehow, I’d installed some PHP 5.6 packages as well!

To solve the problem quickly, I uninstalled PHP 5.6. I could have upgraded everything to 5.6 (or just inactivated them, I suppose), but I just wanted to work on my original problem, not spend my day on system configuration.


Mon, 20 Jul 2015

Sorry.

— SjG @ 7:37 pm

Nervously, Renny “The Cart” Cartesius stood in an ill-fitting uniform before the throng of federal agents, police, and bystanders. To his chagrin, he noticed that “Blazin'” Pascal was in the crowd, and was staring intently with a look of puzzled half-recognition — and then Pascal’s face lit up as he saw through the imperfect disguise.

Knowing that his cover was about to be blown, The Cart took a calculated risk. Hissing them quietly under his breath, he spoke the immortal words: “incognito ergo schtumm!”


Fri, 5 Jun 2015

pfSense Can’t See the Outside World

— SjG @ 11:00 am

We had a static IP address change on a network that had been in operation for about six years. Since we have gradually been migrating services off to third-party hosting, we no longer need a block of local static IP addresses. To save some ca$h, we are down to one static IP — but that necessitated getting a new IP.

At midnight, the change occurred.

I went into the pfSense admin, got rid of all my 1-to-1 NAT mappings, virtual IPs, and all the firewall rules that protected the no-longer-extant servers. And I couldn’t see the outside world.

I couldn’t even ping the gateway.

Plugging a Mac into the same cable, however, and setting the network parameters, and I had immediate glorious interweb access everywhere.

It was perplexing. The pfSense firewall was configured exactly the same as the Mac. Why u no work firewall?

After a bunch of nonsense, I found the problem. I’d set the WAN interface to our new IP address, and specified it as a single IPv4. I thought I was setting the netmask correctly for a single IP:

IPv4 WAN Address: xxx.xxx.xxx.xxx/32

It turns out, I needed to reduce that netmask. That /32 means *all* of the address is the network submask.

For a single IP address, I used /24 (leaving the entire last byte as my address), although /31 should probably work and would lock it to the specific address.
Edit: The key is the netmask has to leave the gateway in the same subnet as your IP. Doh! You can see I don’t do this kind of stuff enough to know what I[‘m talking about.