View Single Post
Old 08 July 2017, 00:57   #32
a/b
Registered User
 
Join Date: Jun 2016
Location: europe
Posts: 1,039
General c/c++ suggestions:
Code:
char *wintitle, bd[3], ac[3], bl[3], wb[3], gz[3], zo[3];
strcpy(ac, "/ac");
strcpy(bl, "/bl");
strcpy(wb, "/wb");
strcpy(gz, "/gz");
strcpy(zo, "/zo");

strcat(flags, zo);

if (!wininfos) {
	free(miscinfo);   //  <= missing, memory leak
	printf("Failed to create window array of structs! Exiting.\n");
	return 1;
    }

if ((printer = printwindows(wininfos, miscinfo)) != 0) {
	free(miscinfo);   //  <= missing, memory leak
	free(wininfos);   //  <= missing, memory leak
	printf("Failed to print windows! Exiting.\n");
	return 1;
    }
You have to allocate 4 bytes for those strings (3 + terminator).
Use strncat instead of strcat to be on the safe(r) side.

Try to use a little different code structure that makes it easier to clean-up partially allocated/initialized stuff. For example, set all pointers/handles/etc. to null/0/..., then at the end, regardless whether you managed to fully initialize everything or not, go through all of them and free/deinitialize those that aren't null/0. That way you also have one common exit point from a function, so it's easier to add new code (otherwise you have to do partial deallocs all over the place).

Last edited by a/b; 08 July 2017 at 01:10. Reason: added more stuff
a/b is offline  
 
Page generated in 0.05307 seconds with 11 queries