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

  • Home
  • SEARCH
  • 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 168825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:31:17+00:00 2026-05-11T12:31:17+00:00

In case of integer overflows what is the result of (unsigned int) * (int)

  • 0

In case of integer overflows what is the result of (unsigned int) * (int) ? unsigned or int? What type does the array index operator (operator[]) take for char*: int, unsigned int or something else?

I was auditing the following function, and suddenly this question arose. The function has a vulnerability at line 17.

// Create a character array and initialize it with init[]  // repeatedly. The size of this character array is specified by  // w*h. char *function4(unsigned int w, unsigned int h, char *init) {     char *buf;     int i;      if (w*h > 4096)         return (NULL);      buf = (char *)malloc(4096+1);     if (!buf)         return (NULL);      for (i=0; i<h; i++)         memcpy(&buf[i*w], init, w);  // line 17      buf[4096] = '\0';      return buf; } 

Consider both w and h are very large unsigned integers. The multiplication at line 9 have a chance to pass the validation.

Now the problem is at line 17. Multiply int i with unsigned int w: if the result is int, it is possible that the product is negative, resulting in accessing a position that is before buf. If the result is unsigned int, the product will always be positive, resulting in accessing a position that is after buf.

It’s hard to write code to justify this: int is too large. Does anyone has ideas on this?

Is there any documentation that specifies the type of the product? I have searched for it, but so far haven’t found anything.

I suppose that as far as the vulnerability is concerned, whether (unsigned int) * (int) produces unsigned int or int doesn’t matter, because in the compiled object file, they are just bytes. The following code works the same no matter the type of the product:

unsigned int x = 10; int y = -10;  printf('%d\n', x * y);  // print x * y in signed integer printf('%u\n', x * y);  // print x * y in unsigned integer 

Therefore, it does not matter what type the multiplication returns. It matters that whether the consumer function takes int or unsigned.

The question here is not how bad the function is, or how to improve the function to make it better. The function undoubtedly has a vulnerability. The question is about the exact behavior of the function, based on the prescribed behavior from the standards.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T12:31:18+00:00Added an answer on May 11, 2026 at 12:31 pm

    To answer your question: the type of an expression multiplying an int and an unsigned int will be an unsigned int in C/C++.

    To answer your implied question, one decent way to deal with possible overflow in integer arithmetic is to use the ‘IntSafe‘ set of routines from Microsoft:

    http://blogs.msdn.com/michael_howard/archive/2006/02/02/523392.aspx

    It’s available in the SDK and contains inline implementations so you can study what they’re doing if you’re on another platform.

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

Sidebar

Related Questions

When programming in a C-like language should one's default integer type be int or
We get compile time error when integer is divided by zero whereas in case
How do I detect the length of an integer? In case I had le:
I have read in many places that unsigned integer overflow is well-defined in C
All operations on standard signed integer types in C (short, int, long, etc) exhibit
I'm curious about conventions for type-punning pointers/arrays in C++. Here's the use case I
Using integer math alone, I'd like to safely average two unsigned ints in C++.
I am trying to loop through an integer array ( integer[] ) in a
Case in point : I've got a handle to a window (for instance, using
Case One: new Date(Date.parse(Jul 8, 2005)); Output: Fri Jul 08 2005 00:00:00 GMT-0700 (PST)

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.