Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:56:18+00:00 2026-05-23T02:56:18+00:00

I had a bug that caused an integer overflow, resulting in wrong (negative) timestamps

  • 0

I had a bug that caused an integer overflow, resulting in wrong (negative) timestamps being written to the database. The code is fixed already, but I want to fix the wrong data, too.

I thought, I could just take the wrong results and add Integer.MAX_VALUE, but that didn’t seem to work, it left me with to high values. I have the offset value in the code snippet below, but the input values are not stored.

The following code reproduces the bug:

@Test
public void testArexxConversion()
{
    // The input values represent seconds since midnight, Jan 1, 2000 UTC
    final int sample = 361450072; // A sample input value drawn from production
    // I use the offset from the UNIX epoch to convert the vakue to UNIX seconds
    final int offset = 946684800; // midnight, Jan 01 2000 UTC in UNIX seconds
    // This was the buggy line in my code, the assertion will fail
    long result = (sample + offset) * 1000;
    // Prints 'Result is negative: -1830153280'
    Assert.assertTrue(result > 0, String.format("Result is negative: %d", result));
    // This is for comparison
    Date dt = new Date(offset * 1000);
    Assert.assertEquals(dt.getTime() + sample * 1000, result);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:56:19+00:00Added an answer on May 23, 2026 at 2:56 am

    How to fix the bug in your database

    To fix the bug in your database you can do the following addition to all the buggy data:

    long new_result = old_buggy_result + 1309965025280L;
    

    The constant number was found like this:

    1. Check the buggy result value
    2. Find what should the correct result value be?
    3. Do an addition to the buggy result value to find the correct `result.

    But this is only possible if you have saved sample and offset in your database or somewhere else.

    Otherwise, it depends on the number of wraps that occured during the original calculation:

    long size_of_int = (long)Math.pow(2, 32);
    int number_of_wraps = 305 // Only correct in your example!
                              // You can't deduct the number of wraps from
                              // the wrong value alone, because that information
                              // is lost in the modulo (the "wrap")
    long correct_number = wrong_number + size_of_int * number_of_wraps;
    

    If the numbers in your database are close enough to your sample value, this means, you can do the above, using 305 as the number of wraps.

    Explanation of the bug (for future readers)

    The operation here:

     (sample + offset) * 1000;
    

    is computed using int and not long. But the result is “too big” to be saved on an int variable. That’s why you have an overflow.

    Change it to:

      ((long) sample + offset) * 1000L;
    

    So now the + and * operations will be done using long values, and the result will be a long value which won’t overflow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have had a bug recently that only manifested itself when the library was
I've had a bug in our software that occurs when I receive a connection
Did you ever had a bug in your code, you could not resolve? I
I'm currently involved in a heavily localised WPF application and we've had a bug
I thought I had seen a bug report about this on the jQuery site,
I had this question earlier and it was concluded it was a bug in
I had used Server Explorer and related tools for graphical database development with Microsoft
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
Had a coworker ask me this, and in my brain befuddled state I didn't
I had been happily coding along on a decent sized solution (just over 13k

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.