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 3875418
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:15:54+00:00 2026-05-19T22:15:54+00:00

For a program that I’m writing, it is useful for me to calculate file

  • 0

For a program that I’m writing, it is useful for me to calculate file sizes, which I calculate by using iostream’s tellg and seekg functions, but this leads to a warning by -Wstack-protector. The following code reproduces the “problem”:

#include <iostream>

std::streamsize get_file_size(std::ifstream& ifs) { // line 12 (in warning, below)
  const std::streamsize start = ifs.tellg();
  ifs.seekg(0,std::ios::end);
  const std::streamsize end = ifs.tellg();
  ifs.seekg(start);
  return (end-start);
}

g++ (flags: -fstack-protector -Wstack-protector, compiler version: 4.4.3 (Ubuntu 4.4.3-4ubuntu5), system: Ubuntu 10.04 x86_64) gives the warning:


f.cc: In function ‘std::streamsize get_file_size(std::ifstream&)’:
f.cc:12: warning: not protecting function: no buffer at least 8 bytes long

(I get the same results when I use GCC 4.5.2, downloaded and compiled from GNU directly.)

Is this expected from how stack smashing protection works (in general or by GCC) and/or how ifstream and seekg/tellg work? If so, can’t this warning be ignored or is there something better that I can do?

Edit:

Actually, some of the code above is redundant. Just to clarify what’s going on:

#include <iostream>

void f1(std::ifstream& ifs) { // line 6
    ifs.tellg();
}

void f2(std::ifstream& ifs) { // line 10
    // call seekg(std::streampos)
    ifs.seekg(0);
}

void f3(std::ifstream& ifs) {
    // call seekg(std::streamoff, std::ios_base::seekdir)
    ifs.seekg(0,std::ios::beg);
}

leads to g++ (same specs as above) warning:


main.cc: In function ‘void f1(std::ifstream&)’:
main.cc:6: warning: not protecting function: no buffer at least 8 bytes long
main.cc: In function ‘void f2(std::ifstream&)’:
main.cc:10: warning: not protecting function: no buffer at least 8 bytes long

Interestingly, f3 does not trigger a warning.

  • 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-19T22:15:54+00:00Added an answer on May 19, 2026 at 10:15 pm

    You might wan’t to see this.

    And the general advice is you really shouldn’t care, especially in your case, when you don’t allocate any internal buffers that can be used to perform buffer overflow attack.

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

Sidebar

Related Questions

I'm writing a program that displays products and the stores at which they can
I am writing a program that gets a HTML file in Android and I
I have a program that I'm writing in Android using Eclipse. It has an
I am creating a program that will be installed using the .net installer project.
I'm writing a program that sends an email out at a client's specific local
I have a program that creates a Windows user account using the NetUserAdd() API
My program that I am writing's purpose arose with this issue: There are two
I've created this program that can encrypt a found file and then it can
I am writing a program that needs to take text input, and modify individual
The program that was created allows users to simply parse a log text file.

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.