2
0

Fix memory leaks, thanks @daltomi - #134.

This commit is contained in:
Maarten Billemont 2016-11-03 10:31:07 -04:00
parent f427e06692
commit d3f2a01da2
3 changed files with 17 additions and 15 deletions

View File

@ -22,10 +22,9 @@ static const char *mpw_templateForType_v0(MPSiteType type, uint16_t seedByte) {
size_t count = 0; size_t count = 0;
const char **templates = mpw_templatesForType( type, &count ); const char **templates = mpw_templatesForType( type, &count );
if (!count) char const *template = count? templates[seedByte % count]: NULL;
return NULL; free( templates );
return template;
return templates[seedByte % count];
} }
static const char mpw_characterFromClass_v0(char characterClass, uint16_t seedByte) { static const char mpw_characterFromClass_v0(char characterClass, uint16_t seedByte) {

View File

@ -78,7 +78,7 @@ static char *homedir(const char *filename) {
return homefile; return homefile;
} }
static char *getlinep(const char *prompt) { static char *getline_prompt(const char *prompt) {
char *buf = NULL; char *buf = NULL;
size_t bufSize = 0; size_t bufSize = 0;
@ -96,7 +96,7 @@ static char *getlinep(const char *prompt) {
int main(int argc, char *const argv[]) { int main(int argc, char *const argv[]) {
// Read the environment. // Read the environment.
char *fullName = getenv( MP_env_fullname ); char *fullName = strdup( getenv( MP_env_fullname ) );
const char *masterPassword = NULL; const char *masterPassword = NULL;
const char *siteName = NULL; const char *siteName = NULL;
MPSiteType siteType = MPSiteTypeGeneratedLong; MPSiteType siteType = MPSiteTypeGeneratedLong;
@ -116,7 +116,7 @@ int main(int argc, char *const argv[]) {
for (int opt; (opt = getopt( argc, argv, "u:P:t:c:V:a:C:vqh" )) != -1;) for (int opt; (opt = getopt( argc, argv, "u:P:t:c:V:a:C:vqh" )) != -1;)
switch (opt) { switch (opt) {
case 'u': case 'u':
fullName = optarg; fullName = strdup( optarg );
break; break;
case 'P': case 'P':
// Do not use this. Passing your master password via the command-line // Do not use this. Passing your master password via the command-line
@ -166,12 +166,12 @@ int main(int argc, char *const argv[]) {
ftl("Unexpected option: %c", opt); ftl("Unexpected option: %c", opt);
} }
if (optind < argc) if (optind < argc)
siteName = argv[optind]; siteName = strdup( argv[optind] );
// Convert and validate input. // Convert and validate input.
if (!fullName && !(fullName = getlinep( "Your full name:" ))) if (!fullName && !(fullName = getline_prompt( "Your full name:" )))
ftl( "Missing full name.\n" ); ftl( "Missing full name.\n" );
if (!siteName && !(siteName = getlinep( "Site name:" ))) if (!siteName && !(siteName = getline_prompt( "Site name:" )))
ftl( "Missing site name.\n" ); ftl( "Missing site name.\n" );
if (siteCounterString) if (siteCounterString)
siteCounter = (uint32_t)atol( siteCounterString ); siteCounter = (uint32_t)atol( siteCounterString );
@ -210,21 +210,27 @@ int main(int argc, char *const argv[]) {
masterPassword = getpass( "Your master password: " ); masterPassword = getpass( "Your master password: " );
// Summarize operation. // Summarize operation.
fprintf( stderr, "%s's password for %s:\n[ %s ]: ", fullName, siteName, mpw_identicon( fullName, masterPassword ) ); const char *identicon = mpw_identicon( fullName, masterPassword );
fprintf( stderr, "%s's password for %s:\n[ %s ]: ", fullName, siteName, identicon );
mpw_free_string( identicon );
// Output the password. // Output the password.
const uint8_t *masterKey = mpw_masterKeyForUser( const uint8_t *masterKey = mpw_masterKeyForUser(
fullName, masterPassword, algorithmVersion ); fullName, masterPassword, algorithmVersion );
mpw_free_string( masterPassword ); mpw_free_string( masterPassword );
mpw_free_string( fullName );
if (!masterKey) if (!masterKey)
ftl( "Couldn't derive master key." ); ftl( "Couldn't derive master key." );
const char *sitePassword = mpw_passwordForSite( const char *sitePassword = mpw_passwordForSite(
masterKey, siteName, siteType, siteCounter, siteVariant, siteContextString, algorithmVersion ); masterKey, siteName, siteType, siteCounter, siteVariant, siteContextString, algorithmVersion );
mpw_free( masterKey, MP_dkLen ); mpw_free( masterKey, MP_dkLen );
mpw_free_string( siteName );
if (!sitePassword) if (!sitePassword)
ftl( "Couldn't derive site password." ); ftl( "Couldn't derive site password." );
fprintf( stdout, "%s\n", sitePassword ); fprintf( stdout, "%s\n", sitePassword );
mpw_free_string( sitePassword );
return 0; return 0;
} }

View File

@ -112,10 +112,7 @@ const char *mpw_templateForType(MPSiteType type, uint8_t seedByte) {
size_t count = 0; size_t count = 0;
const char **templates = mpw_templatesForType( type, &count ); const char **templates = mpw_templatesForType( type, &count );
if (!count) char const *template = count? templates[seedByte % count]: NULL;
return NULL;
char const *template = templates[seedByte % count];
free( templates ); free( templates );
return template; return template;
} }