ALSA: info: Small refactoring and a sanity check in snd_info_get_line()
authorTakashi Iwai <tiwai@suse.de>
Wed, 13 Mar 2013 11:11:13 +0000 (12:11 +0100)
committerTakashi Iwai <tiwai@suse.de>
Wed, 13 Mar 2013 11:11:13 +0000 (12:11 +0100)
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/core/info.c

index 58e97b35ccebbc6d095e38e8dc6547b14c3c19ca..c9042b4d3695f32a97649dc9eaa0d84e569c5d68 100644 (file)
@@ -700,26 +700,21 @@ int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
 {
        int c = -1;
 
+       if (snd_BUG_ON(!buffer || !buffer->buffer))
+               return 1;
        if (len <= 0 || buffer->stop || buffer->error)
                return 1;
-       while (--len > 0) {
+       while (!buffer->stop) {
                c = buffer->buffer[buffer->curr++];
-               if (c == '\n') {
-                       if (buffer->curr >= buffer->size)
-                               buffer->stop = 1;
-                       break;
-               }
-               *line++ = c;
-               if (buffer->curr >= buffer->size) {
+               if (buffer->curr >= buffer->size)
                        buffer->stop = 1;
+               if (c == '\n')
                        break;
+               if (len) {
+                       len--;
+                       *line++ = c;
                }
        }
-       while (c != '\n' && !buffer->stop) {
-               c = buffer->buffer[buffer->curr++];
-               if (buffer->curr >= buffer->size)
-                       buffer->stop = 1;
-       }
        *line = '\0';
        return 0;
 }