Variable 'offset' can be destroyed by the time it's ready to be used

freixas - January 5, 2008 - 00:35
Project:Auto Time Zone
Version:5.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review
Description

Auto Time Zone uses the $(document).ready() function to register a callback that sets the time zone after the Web pages loads.

The variable offset is calculated when the module begins loading and is used by the callback. However, there is no guarantee that some other Javascript code won't use the variable 'offset' (a fairly common word) for some other purpose. This could generate a corrupt timezone value. It happened to me.

The best solution is to recalculate the offset in the callback. Instead of using

{$(document).ready(function(){$.get(' . drupal_to_js(url('autotimezone/', NULL, NULL, TRUE)) . ' + offset);})}';

use
{$(document).ready(function(){$.get(' . drupal_to_js(url('autotimezone/', NULL, NULL, TRUE)) . ' + new Date().getTimezoneOffset());})}';

It's a pretty easy fix.

#1

freixas - January 5, 2008 - 00:36

#2

paul.lovvik - July 22, 2008 - 18:24

Also it is important not to pollute the global namespace with temporary variables. Even though 'var' is used for the 'now' and 'offset' variables, they are still global due to the scope in which they are created. I have created a patch that prevents this by declaring them within the scope of the anonymous function.

AttachmentSize
206494_2.patch 1.21 KB

#3

paul.lovvik - July 23, 2008 - 12:04
Status:active» needs review
 
 

Drupal is a registered trademark of Dries Buytaert.