Jump to content

Can you get scanf_s and similar functions to work in C on linux?

AndreiArgeanu
Go to solution Solved by wanderingfool2,
On 11/13/2023 at 5:49 AM, AndreiArgeanu said:

I was reading some C documentation for the scan functions (scanf, fscanf, sscanf) and saw the _s (scanf_s ...) which are security enhanced versions of the previous functions, however those functions aren't on Linux, they seem to be MS exclusive I think part of MSVC. Any reason why? Is there an easy way to get them on linux as well? I would like to have the enhancements on Linux as well without having to write them myself.

I would just write your own versions, it's not part of the standard unfortunately.

 

6 hours ago, wasab said:

please just use the POSIX apis for linux. microsoft implemented most of these posix interfaces in its linux subsystem and plenty posix compilers exist for windows so not like you are missing out on anything should you go back to windows. you can in fact write a cross-platform application and cross-compiling if you stick closely to the c standard library and its posix extensions, for the most part anyway.

While I do think that cross compatibility is good; I think there is a general issue in terms of telling people to use POSIX version when their question is around using the MS's _s versions.

 

The reason being that things like sscanf, snscanf, etc have their own little quirks to them that make them unsafe (or rather it's a lot easier to make a simple mistake while using them).  It's things where you have to use it and write code to properly handle the different scenarios...the thing is 99% of the time you see examples online that shows examples in an unsafe manor and unless you actually know about it you might not realize you are missing it in your code (especially for beginners)...as I think that's where tons of issues can arise from.

 

As an example

strncpy(buff, copy, buff_size); //Issue is if len(copy) == buff_size the null pointer is not copied.

strncpy_s however fixes that by always making sure buff would end up with a null termination

 

I was reading some C documentation for the scan functions (scanf, fscanf, sscanf) and saw the _s (scanf_s ...) which are security enhanced versions of the previous functions, however those functions aren't on Linux, they seem to be MS exclusive I think part of MSVC. Any reason why? Is there an easy way to get them on linux as well? I would like to have the enhancements on Linux as well without having to write them myself.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...

please just use the POSIX apis for linux. microsoft implemented most of these posix interfaces in its linux subsystem and plenty posix compilers exist for windows so not like you are missing out on anything should you go back to windows. you can in fact write a cross-platform application and cross-compiling if you stick closely to the c standard library and its posix extensions, for the most part anyway.

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

On 11/13/2023 at 5:49 AM, AndreiArgeanu said:

I was reading some C documentation for the scan functions (scanf, fscanf, sscanf) and saw the _s (scanf_s ...) which are security enhanced versions of the previous functions, however those functions aren't on Linux, they seem to be MS exclusive I think part of MSVC. Any reason why? Is there an easy way to get them on linux as well? I would like to have the enhancements on Linux as well without having to write them myself.

I would just write your own versions, it's not part of the standard unfortunately.

 

6 hours ago, wasab said:

please just use the POSIX apis for linux. microsoft implemented most of these posix interfaces in its linux subsystem and plenty posix compilers exist for windows so not like you are missing out on anything should you go back to windows. you can in fact write a cross-platform application and cross-compiling if you stick closely to the c standard library and its posix extensions, for the most part anyway.

While I do think that cross compatibility is good; I think there is a general issue in terms of telling people to use POSIX version when their question is around using the MS's _s versions.

 

The reason being that things like sscanf, snscanf, etc have their own little quirks to them that make them unsafe (or rather it's a lot easier to make a simple mistake while using them).  It's things where you have to use it and write code to properly handle the different scenarios...the thing is 99% of the time you see examples online that shows examples in an unsafe manor and unless you actually know about it you might not realize you are missing it in your code (especially for beginners)...as I think that's where tons of issues can arise from.

 

As an example

strncpy(buff, copy, buff_size); //Issue is if len(copy) == buff_size the null pointer is not copied.

strncpy_s however fixes that by always making sure buff would end up with a null termination

 

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×