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;
const char **templates = mpw_templatesForType( type, &count );
if (!count)
return NULL;
return templates[seedByte % count];
char const *template = count? templates[seedByte % count]: NULL;
free( templates );
return template;
}
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;
}
static char *getlinep(const char *prompt) {
static char *getline_prompt(const char *prompt) {
char *buf = NULL;
size_t bufSize = 0;
@ -96,7 +96,7 @@ static char *getlinep(const char *prompt) {
int main(int argc, char *const argv[]) {
// Read the environment.
char *fullName = getenv( MP_env_fullname );
char *fullName = strdup( getenv( MP_env_fullname ) );
const char *masterPassword = NULL;
const char *siteName = NULL;
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;)
switch (opt) {
case 'u':
fullName = optarg;
fullName = strdup( optarg );
break;
case 'P':
// 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);
}
if (optind < argc)
siteName = argv[optind];
siteName = strdup( argv[optind] );
// Convert and validate input.
if (!fullName && !(fullName = getlinep( "Your full name:" )))
if (!fullName && !(fullName = getline_prompt( "Your full name:" )))
ftl( "Missing full name.\n" );
if (!siteName && !(siteName = getlinep( "Site name:" )))
if (!siteName && !(siteName = getline_prompt( "Site name:" )))
ftl( "Missing site name.\n" );
if (siteCounterString)
siteCounter = (uint32_t)atol( siteCounterString );
@ -210,21 +210,27 @@ int main(int argc, char *const argv[]) {
masterPassword = getpass( "Your master password: " );
// 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.
const uint8_t *masterKey = mpw_masterKeyForUser(
fullName, masterPassword, algorithmVersion );
mpw_free_string( masterPassword );
mpw_free_string( fullName );
if (!masterKey)
ftl( "Couldn't derive master key." );
const char *sitePassword = mpw_passwordForSite(
masterKey, siteName, siteType, siteCounter, siteVariant, siteContextString, algorithmVersion );
mpw_free( masterKey, MP_dkLen );
mpw_free_string( siteName );
if (!sitePassword)
ftl( "Couldn't derive site password." );
fprintf( stdout, "%s\n", sitePassword );
mpw_free_string( sitePassword );
return 0;
}

View File

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