Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for issue 685 #122

Merged
merged 4 commits into from
Apr 27, 2017
Merged

fix for issue 685 #122

merged 4 commits into from
Apr 27, 2017

Conversation

bingbing8
Copy link

@bingbing8 bingbing8 commented Apr 25, 2017

fix for issue that ssh-keygen exit after prompt the user if he/she want to override the existing file.
PowerShell/Win32-OpenSSH#685

ssh-keygen prompt the user if he/she want to override the existing file, but exit
cp++;
} while ((actual_read < n - 1) && *str_tmp != '\n');

if (actual_read > n - 1) {
/* shouldn't happen. but handling in case */
errno = EINVAL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a error log

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

}
else
break;
cp++;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cp should be incremented by the strlen(str_tmp)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

if((actual_read + strlen(str_tmp)) < n) {
memcpy(cp, str_tmp, strlen(str_tmp) + 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why +1? is it for '\0'?
If yes then we can do it in the end before returning.. not required for every byte.. this might confuse us.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, for '\0'.

@@ -283,33 +283,49 @@ char*
w32_fgets(char *str, int n, FILE *stream) {
HANDLE h = (HANDLE)_get_osfhandle(_fileno(stream));
wchar_t* str_w = NULL;
char *ret = NULL, *str_tmp = NULL;
char *ret = NULL, *str_tmp = NULL, *cp = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ret is not required. we can return str itself?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, according to https://linux.die.net/man/3/fgets, only return str on success; will return NULL on error

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, ret variable is not required.. we can still use str variable itself..at the start of the function initialize str to NULL and everywhere return str..
Its a minor comment, fix as you like.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, str is pre-allocated memory in caller function

goto cleanup;
}

if((actual_read + strlen(str_tmp)) < n) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this if/else and add it as part of the while condition..

} while ( (actual_read = (actual_read + strlen(str_tmp)) && *str_tmp != '\n');

though line 321 is required.

Copy link
Author

@bingbing8 bingbing8 Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we need to check it before copying. strlen(str_tmp) may be more than the remaining length of str, we may copy more than n.

@@ -283,33 +283,49 @@ char*
w32_fgets(char *str, int n, FILE *stream) {
HANDLE h = (HANDLE)_get_osfhandle(_fileno(stream));
wchar_t* str_w = NULL;
char *ret = NULL, *str_tmp = NULL;
char *ret = NULL, *str_tmp = NULL, *cp = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, ret variable is not required.. we can still use str variable itself..at the start of the function initialize str to NULL and everywhere return str..
Its a minor comment, fix as you like.

@bingbing8 bingbing8 changed the title fix for issue 685 fix for issue 685 (https://github.com/PowerShell/Win32-OpenSSH/issues/685) Apr 27, 2017
@bingbing8 bingbing8 changed the title fix for issue 685 (https://github.com/PowerShell/Win32-OpenSSH/issues/685) fix for issue 685 Apr 27, 2017
free(str_tmp);
if (fgetws(str_w, 2, stream) == NULL)
goto cleanup;
if ((str_tmp = utf16_to_utf8(str_w)) == NULL) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utf16_to_utf8 will alloc memory for each call. To optimize, you may create a static buffer (to accommodate 2 utf-8 character, max 8 bytes) and call WideCharToMultiByte directly)

do {
if (str_tmp)
free(str_tmp);
if (fgetws(str_w, 2, stream) == NULL)
Copy link

@manojampalam manojampalam Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work for UTF-16 charactors that will occupy 2 wide chars (or 4 bytes)

Copy link

@manojampalam manojampalam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a new issue to address comments below

@manojampalam manojampalam merged commit d7ab0aa into latestw_all Apr 27, 2017
@manojampalam manojampalam deleted the Issue684 branch April 27, 2017 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants