Is there a way to catch a STATUS_STACK_BUFFER_OVERRUN error programmatically? - seh

We have some C code that is throwing a STATUS_STACK_BUFFER_OVERRUN error (0xC0000409) once in a while. I can reproduce that error using the C code below. I'm using Visual Studio 2013 Update 4 on Windows 7, and I'm compiling with the /EHa and /GS flags. However, I have been unable to catch the error programmatically. The code never enters my __except block; instead, Visual Studio pops up a few dialog boxes informing me of the stack corruption. I realize that once this error occurs, the state of the program is in doubt; I'm merely trying to capture the error in hopes of locating where it is occurring in our production code. Is there a way to handle this error programmatically?
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#pragma warning(disable: 4996) // strcpy
void vulnerable(const char* str)
{
char buffer[10];
strcpy(buffer, str); // overrun the buffer
}
int main()
{
__try
{
char large_buffer[] = "This string is longer than 10 characters.";
vulnerable(large_buffer);
}
__except (GetExceptionCode() == STATUS_STACK_BUFFER_OVERRUN)
{
printf("error"); // never getting here
}
}

Related

Is there any way to disable Screen Capture/Screen Recording Flutter win32 app

I am trying to secure my application, Is there any way of disabling screen capture in win32 application.
Flutter for windows
Thanks in advance
Output I desire
I figured out how to disable screen capture in Flutter Windows Application
API Reference: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowdisplayaffinity
Navigate to Project/windows/runner/main.cpp
Add the below code in wWinMain method:
//DISABLE SCREEN
HWND code =window.GetHandle();
SetWindowDisplayAffinity(code, 0x00000011);
main.cpp:
#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>
#include "flutter_window.h"
#include "utils.h"
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command) {
// Attach to console when present (e.g., 'flutter run') or create a
// new console when running with a debugger.
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
CreateAndAttachConsole();
}
// Initialize COM, so that it is available for use in the library and/or
// plugins.
::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
flutter::DartProject project(L"data");
std::vector<std::string> command_line_arguments =
GetCommandLineArguments();
project.set_dart_entrypoint_arguments(std::move(command_line_arguments));
FlutterWindow window(project);
Win32Window::Point origin(10, 10);
Win32Window::Size size(1280, 720);
if (!window.CreateAndShow(L"windows_app", origin, size)) {
return EXIT_FAILURE;
}
//DISABLE SCREEN
HWND code =window.GetHandle();
SetWindowDisplayAffinity(code, 0x00000011);
window.SetQuitOnClose(true);
::MSG msg;
while (::GetMessage(&msg, nullptr, 0, 0)) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
::CoUninitialize();
return EXIT_SUCCESS;
}

Why can I not print non-ASCII characters to the console in curses mode?

Please consider this simple snippet:
#include <stdio.h>
#include <curses.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
void cleanup(int signal)
{
endwin();
exit(0);
}
int main()
{
initscr();
struct sigaction cleanup_action = { .sa_handler = cleanup, .sa_flags = 0 };
sigfillset(&cleanup_action.sa_mask);
sigaction(SIGINT, &cleanup_action, NULL);
cbreak();
keypad(stdscr, TRUE);
noecho();
mvaddstr(2, 2, "🧱");
mvaddstr(2, 3, "🧱");
mvaddstr(2, 4, "🧱");
mvaddstr(3, 2, "⬜");
mvaddstr(3, 2, "âš¾");
mvaddstr(3, 4, "⬜");
refresh();
while(true) getch();
return 0;
}
(No, I'm not certain that my clean-up on exit is correct, but that's not the point.)
Why are the emojis not being printed out?
When I run this program this is what I see:
���~_��
�~��~\
I don't understand this because according to POSIX specification:
addnstr, addstr, mvaddnstr, mvaddstr, mvwaddnstr, mvwaddstr waddnstr, waddstr - add a string of multi-byte characters without rendition to a window and advance cursor
"MULTI-BYTE" they say! So I guess this should print out correctly! I'm not limited to ASCII!
Also, I guess my terminal can handle these characters. This is because as opposed to curses.h, stdio.h is able to print them correctly:
#include <stdio.h>
int main()
{
printf("🧱⬜⚾\n");
return 0;
}
This prints out:
🧱⬜⚾
How can I print emojis with curses.h?

Timer0 as counter with Microchip PIC10F202

I developing RC switch for my RC glider.
I have microkontroller Microchip PIC10F202 and I have problem with ON/OFF LED Lights.
I want triggering input PPM signal from RC receiver, I setted GP2 as input (by T0CKI and count for every rise edge - from low to high), but LED is still ON, not reacting on input signal from RC receiver.
I post my source code in C language, I can not ASM language.
// this is header file with some macros and function prototypes
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
#include <xc.h>
#define _XTAL_FREQ 4000000
#define KONST_ON 50
#define KONST_OFF 1000
#define STROBO_LED GP0
#define NAVI_LED GP1
#define RC_SIGNAL GP2
#define STAV_ON 1
#define STAV_OFF 0
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
void setup ();
void flashing ();
#endif
// In this function is setup for microkontroller
#include <xc.h>
#include "prototypy.h"
void setup ()
{
OSCCAL = 0b0111111;
//STATUS = 0b00111000;
OPTION = 0b11100111;
TRISGPIO = 0b0000;
};
// This is function for LED flashing
#include <xc.h>
#include "prototypy.h"
void flashing ()
{
__delay_ms (KONST_OFF);
STROBO_LED = STAV_OFF;
__delay_ms (KONST_ON);
STROBO_LED = STAV_ON;
};
// in this main function I do logical product, where I have logical state from pin GP2 and number 1
#include <xc.h>
#include "prototypy.h"
// Code protection bit
#pragma config CP = OFF
// Watchdog timer
#pragma config WDTE = OFF
// Pin function
#pragma config MCLRE = OFF
void main ()
{
setup ();
while (1)
{
if (RC_SIGNAL & 1)
{
flashing ();
}
}
}
Please, can me someone help and found error in my source code?
I'am not really familar with that devive, but I guess you Pin RC_SIGNAL is not configured as an Input. Please try
TRISGPIO = 0b0100;

scanf_s doesn't work visual studio

#include <windows.h>
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
vis studio 2017,
doesn't matter what i do,
int main(){
char c = 's';
scanf_s(" %c", &c, 1);
}
simple program, changing scanf_s line to
scanf_s("%c", &c, 1);
again to,
scanf_s("%1c", &c, 1);
etc nothing works I get a debug error
scanf_s(" %1c", &c, sizeof(c));
again, error, I don't know what the problem is
scanf_s("%c",&c);
it brings me to this line in debugger:
{
return __stdio_common_vfscanf(
_CRT_INTERNAL_LOCAL_SCANF_OPTIONS | _CRT_INTERNAL_SCANF_SECURECRT,
_Stream, _Format, _Locale, _ArgList);
}
#endif
I created a new project and it fixed the problem

How can you get Eclipse CDT to understand MSPGCC (MSP430) includes?

I'm using Eclipse and CDT to work with the mspgcc compiler, it compiles fine, but the code view highlights all my special function registers as unresolved.
I've made a C project where the compiler is "msp430-gcc -mmcu=msp430x2012", and that's set to look for includes in /usr/msp430/include/. I've set the linker to "msp430-gcc -mmcu=msp430x2012", and that's set to look fo libraries in /usr/msp430/lib/. I've set the assembler to "msp430-as". I've told eclipse it's making an elf and I've disabled automatic includes discovery to not find the i686 libraries on my linux box (stupid eclipse!).
Here's the code:
#include <msp430.h>
#include <signal.h> //for interrupts
#define RED 1
#define GREEN 64
#define S2VAL 8
void init(void);
int main(void) {
init(); //Setup Device
P1OUT = GREEN; //start with a green LED
_BIS_SR(LPM4_bits); //Go into Low power mode 4, main stops here
return(1); //never reached, surpresses compiler warning
}
interrupt (PORT1_VECTOR) S1ServiceRoutine(void) {
//we wake the MCU here
if (RED & P1IN) {
P1OUT = GREEN;
} else {
P1OUT = RED;
}
P1IFG = 0; //clear the interrupt flag or we immidiately go again
//we resume LPM4 here thanks to the RETI instruction
}
void init(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
/*Halt the watchdog timer
P1DIR = ~S2VAL; //Set LED pins as outputs and S2 as input
P1IES = S2VAL; //interrupt on High to Low
P1IE = S2VAL; //enable interrupt for S1 only
WRITE_SR(GIE); //enable maskable interrupts
}
All the variables defines in the mspgcc includes such as P1OUT and WDTCTL show up in the problems box as "not resolved", but remember it builds just fine. I've even tried explicitly including the header file for my chip (normally msp430-gcc does this via msp430.h and the -mmcu option).
I resolved this issue by explicitly including the msp430g2553.h file
#include <msp430g2553.h>
I resolved the issue by following the instructions here